十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
在javascript中eval()可以實現(xiàn)字符串轉代碼,java中需要使用動態(tài)編譯。

站在用戶的角度思考問題,與客戶深入溝通,找到龍亭網(wǎng)站設計與龍亭網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站制作、成都外貿網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務覆蓋龍亭地區(qū)。
把獲得的字符串寫入一個臨時文件中,然后編譯它,在調用其中的函數(shù)。
我們把要轉換的字符串構造一個完整的類:如果方法是有返回值的.則:
public object eval(string str){
//生成java文件
string s = "class temp{";
s += "object rt(){"
s += "myclass mc = new myclass();"
s += " return mc."+str+"();";
s += "}"
s +="}";
file f = new file("temp.java");
printwriter pw = new printwriter(new filewriter(f));
pw.println(s);
pw.close();
//動態(tài)編譯
com.sun.tools.javac.main javac = new com.sun.tools.javac.main();
string[] cpargs = new string[] {"-d", "所在目錄","temp.java"};
int status = javac.compile(cpargs);
if(status!=0){
system.out.println("沒有成功編譯源文件!");
return null;
}
//調用temp的rt方法返回結果:
myclassloader mc = new myclassloader();
class clasz = mc.loadclass("test.class",true);
method rt = clasz.getmethod("rt", new class[]{ string[].class });
return rt.invoke(null, new object[] { new string[0] });
//如果方法沒有返回就直接調用
}
我們可以先寫好多個重載的eval,有返回值和沒有返回值的.以及可以傳遞參數(shù)的.
這樣我們就可以用字符串轉換為java的語句來執(zhí)行.
//按回車鍵就可以
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Print {
public static void main(String[] args) {
JFrame jFrame = new JFrame ();
final JLabel jLabel = new JLabel ("按回車鍵!");
jFrame.setLayout(null);
jLabel.setBounds(80,50,500,80);
jFrame.add(jLabel);
jFrame.setSize(200, 200);
jFrame.setLocation(200, 300);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.addKeyListener(new KeyAdapter () {
int n = 0;
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER) {
n++;
if (n == 1)
jLabel.setText("綠");
else if (n == 2) {
jLabel.setText("紅");
} else if (n == 3) {
jLabel.setText("黃");
n = 0;
}
}
}
});
}
}
package threadgroup;
class ThreadDemo3 extends Thread {
private String name;
private int delay;
public ThreadDemo3(String sname, int i_delay) {
name = sname;
delay = i_delay;
}
public void run() {
try {
sleep(delay);
} catch (InterruptedException e) {
}
System.out.println("多線程測試!\n" + name + "\n" + delay);
}
}
public class testMyThread {
public static void main(String[] args) {
ThreadDemo3 th1,th2,th3;
th1 = new ThreadDemo3("線程1", (int) (Math.random() * 900));
th2 = new ThreadDemo3("線程2", (int) (Math.random() * 900));
th3 = new ThreadDemo3("線程3", (int) (Math.random() * 900));
th1.start();
th2.start();
th3.start();
}
}
package threadgroup;
public class threadDemo {
public static void main(String[] args) {
Thread t = Thread.currentThread();
t.setName("你好嗎?");
System.out.println("正在進行的Thread是:" + t);
try {
for (int i = 0; i 5; i++) {
System.out.println("我不叫穆繼超" + i);
Thread.sleep(3000);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
}
}
package threadgroup;
public class threadDemo2 implements Runnable {
public threadDemo2() {
Thread t1 = Thread.currentThread();
t1.setName("第一個主進程");
System.out.println("正在運行" + t1);
Thread t2 = new Thread(this, "");
System.out.println("在創(chuàng)建一個進程");
t2.start();
try {
System.out.println("使他進入第一個睡眠狀態(tài)");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第一個進程");
}
public void run() {
try {
for (int i = 0; i 5; i++) {
System.out.println("進程" + i);
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第二個進程");
}
public static void main(String[] args) {
new threadDemo2();
}
}