再网上down bsh这个框架 然后import bsh.Interpreter;public class Test { public static void main(String args[]) { System.out.println(System.currentTimeMillis()); Interpreter interpreter = new Interpreter(); String s = "public class Hello {"; s += " public Hello() {System.out.println(\"Hello\");}"; s += " public void sayHello() {System.out.println(\"Hello\");}"; s += "}"; String test = "abc"; try { interpreter.eval(s); interpreter.eval("Hello " + test + " = new Hello()"); interpreter.eval("abc.sayHello()"); } catch (Throwable t) { t.printStackTrace(); } System.out.println(System.currentTimeMillis()); } }可以做到动态调用
1.改变设计,这种设计很诡异,我会的几门语言没有支持这个特性的.
2.使用Map,在你别的帖子里说过了.
3.使用java6提供的编译器API,把String的内容和class A的内容组织到一个编译单元中,动态调用编译器的功能.可以参考:
http://www.ibm.com/developerworks/cn/java/j-lo-jse64/
然后import bsh.Interpreter;public class Test
{ public static void main(String args[])
{
System.out.println(System.currentTimeMillis());
Interpreter interpreter = new Interpreter();
String s = "public class Hello {";
s += " public Hello() {System.out.println(\"Hello\");}";
s += " public void sayHello() {System.out.println(\"Hello\");}";
s += "}";
String test = "abc";
try
{
interpreter.eval(s);
interpreter.eval("Hello " + test + " = new Hello()");
interpreter.eval("abc.sayHello()");
}
catch (Throwable t)
{
t.printStackTrace();
}
System.out.println(System.currentTimeMillis());
}
}可以做到动态调用
好像JDK5.0以上还有一个枚举型的,应该用的着吧
1.改变设计,这种设计很诡异,我会的几门语言没有支持这个特性的.
2.使用Map,在你别的帖子里说过了.
3.使用java6提供的编译器API,把String的内容和class A的内容组织到一个编译单元中,动态调用编译器的功能