java行车代码,基本java代码
java怎么把一段字符串当做代码来执行
在javascript中eval()可以实现字符串转代码,java中需要使用动态编译。
站在用户的角度思考问题,与客户深入沟通,找到龙亭网站设计与龙亭网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、网站制作、成都外贸网站建设、企业官网、英文网站、手机端网站、网站推广、域名注册、网页空间、企业邮箱。业务覆盖龙亭地区。
把获得的字符串写入一个临时文件中,然后编译它,在调用其中的函数。
我们把要转换的字符串构造一个完整的类:如果方法是有返回值的.则:
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();
//动态编译
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,有返回值和没有返回值的.以及可以传递参数的.
这样我们就可以用字符串转换为java的语句来执行.
java 红绿灯 代码
//按回车键就可以
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;
}
}
}
});
}
}
java线程的经典代码
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("在创建一个进程");
t2.start();
try {
System.out.println("使他进入第一个睡眠状态");
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();
}
}
文章标题:java行车代码,基本java代码
文章出自:http://azwzsj.com/article/heciph.html