A

目前創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計、鹿邑網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
1.構(gòu)造函數(shù)可以是private的
2.super(1);會調(diào)用父類的Base(int i) 構(gòu)造函數(shù),輸出1
Course.java
public?class?Course?{
private?String?name;//課程名
private?String?id;?//編號
private?String?selectId;?//選修課編號
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?String?getId()?{
return?id;
}
public?void?setId(String?id)?{
this.id?=?id;
}
public?String?getSelectId()?{
return?selectId;
}
public?void?setSelectId(String?selectId)?{
this.selectId?=?selectId;
}
}
java字符串如何解析成運(yùn)行的java代碼
有些情況下,不得不動態(tài)運(yùn)行Java代碼,以便提供更加靈活的方式,以下代碼可參考(在JDK 1.5+平臺上運(yùn)行通過):
public static void main(String[] args) {
int i = 10;
String code = "System.out.println(\"Hello World!\"+(13+2*5/3));";
code += "for(int i=0;i" + i + ";i++){";
code += " System.out.println(Math.pow(i,2));";
code += "}";
try {
run(code);
} catch (Exception e) {
e.printStackTrace();
}
}
private synchronized static File compile(String code) throws Exception {
File file = File.createTempFile("JavaRuntime", ".java", new File(System.getProperty("user.dir")));
file.deleteOnExit();
// 獲得類名
String classname = getBaseFileName(file);
// 將代碼輸出到文件
PrintWriter out = new PrintWriter(new FileOutputStream(file));
out.println(getClassCode(code, classname));
out.close();
// 編譯生成的java文件
String[] cpargs = new String[] { "-d",
System.getProperty("user.dir") + "\\WebRoot\\WEB-INF\\classes",
file.getName() };
int status = Main.compile(cpargs);
if (status != 0) {
throw new Exception("語法錯誤!");
}
return file;
}
private static synchronized void run(String code) throws Exception {
String classname = getBaseFileName(compile(code));
new File(System.getProperty("user.dir")
+ "\\WebRoot\\WEB-INF\\classes\\" + classname + ".class")
.deleteOnExit();
try {
Class cls = Class.forName(classname);
Method main = cls.getMethod("method", null);
main.invoke(cls, null);
} catch (Exception se) {
se.printStackTrace();
}
}
private static String getClassCode(String code, String className) {
StringBuffer text = new StringBuffer();
text.append("public class " + className + "{\n");
text.append(" public static void method(){\n");
text.append(" " + code + "\n");
text.append(" }\n");
text.append("}");
return text.toString();
}
private static String getBaseFileName(File file) {
String fileName = file.getName();
int index = fileName.indexOf(".");
String result = "";
if (index != -1) {
result = fileName.substring(0, index);
} else {
result = fileName;
}
return result;
}
.java文件的話,非項目那種單篇幅的源代碼需要先進(jìn)行編譯,生成.class文件可以在命令控制臺下用java 文件名 進(jìn)行運(yùn)行,編譯java文件需要javac.exe程序 應(yīng)該是jdk中的工具,所以你需要下載jdk并配置環(huán)境變量,然后在控制臺運(yùn)行javac編譯源文件所在目錄下的java文件,會在本目錄下生成一個同名的.class文件
(沒有報錯的情況下) ,然后運(yùn)行java 文件名 即可運(yùn)行該代碼(前提是你這篇文件需要有main方法)。
當(dāng)前題目:java可運(yùn)行代碼,java運(yùn)行代碼為啥前面會有個叉叉
文章起源:http://chinadenli.net/article11/dsgdgdd.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、電子商務(wù)、企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化、網(wǎng)頁設(shè)計公司、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)