如何使用变量名运行.java文件 [英] How to run .java file with Variable Name

查看:93
本文介绍了如何使用变量名运行.java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个基于Pebble Smartwatch操作系统的程序。我有一个名为 PebbleOS_Faces.txt 的文本文件,它具有手表上不同表面的所有名称。



我使用 PrintWriter 将内容转换为.java 文件,然后我使用从此帖获得的代码编译它。它被编译到所有其他类旁边的 build / classes / pebbleos 文件夹(我使用Netbeans for Mac,以防万一有所不同)。我已经确认这一切都可以正常工作。



剩下的是让我从程序内部实际运行类。事情是,每个类将有一个不同的名称,基于 PebbleOS_Faces.txt 文件中的表面的名称。如何创建一个具有变量名的类的实例?

解决方案

编译的类,但是...



尝试使用Class.forName(com.example.YourClassName)获取对类的引用:



http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName(java.lang.String)



然后使用Class.newInstance()创建类的实例:



http://docs.oracle.com/javase/7/docs/api/java/ lang / Class.html#newInstance()



要使这个工作,com.example.YourClassName必须对类加载器可见。



例如:

  Class clazz = Class.forName com.example.YourClassName); 
Object instance = clazz.newInstance();

只有当你的类有一个无参数的构造函数时,newInstance >

如果YourClassName类的构造函数需要参数,则必须使用稍微不同的技术来调用特定的构造函数并将值传递给它。例如,如果你有这样的构造函数:



YourClassName(Integer someInt,String someString)



您可以通过该构造函数实例化YourClassName:

  Class clazz = Class.forName(com.example.YourClassName) ; 
构造函数constructor = clazz.getConstructor(Integer.class,String.class);
Object instance = constructor.newInstance(anInteger,aString);

如果YourClassName的每个实例都实现了相同的 interface 或扩展相同的基类,以便可以将newInstance()的返回值强制转换到该接口或基类。然后你就可以在实例上调用接口或基类的方法。否则,您在对象上调用有用方法的唯一方法是使用反射


I'm trying to make a program based off of the Pebble Smartwatch Operating System. I have a text file called PebbleOS_Faces.txt which has all the names of the different watch faces I will have on the watch.

I use PrintWriter to convert the content into a .java file, and then I compile it using the code I got from this post. It gets compiled into the build/classes/pebbleos folder (I'm using Netbeans for Mac, in case that makes a difference), next to all of the other classes. I've confirmed that this all works properly.

What's left is for me to actually run the class from inside the program. The thing is, each class would have a different name based off of the names of the watch faces in the PebbleOS_Faces.txt file. How do I create an instance of a class that has a variable name?

解决方案

I've never tried this with dynamically generated/compiled classes, but...

Try using Class.forName("com.example.YourClassName") to get a reference to the Class:

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName(java.lang.String)

and then use Class.newInstance() to create an instance of the class:

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance()

For this to work, com.example.YourClassName would have to be visible to your class loader.

For example:

Class clazz = Class.forName("com.example.YourClassName");
Object instance = clazz.newInstance();

The newInstance() call will only work if your class has a no-argument constructor.

If the YourClassName class's constructor requires arguments you must use a slightly different technique to call a specific constructor and pass values into it. For example, if you had a constructor like this:

YourClassName(Integer someInt, String someString)

Then you could do this to instantiate YourClassName via that constructor:

Class clazz = Class.forName("com.example.YourClassName");
Constructor constructor = clazz.getConstructor(Integer.class, String.class);
Object instance = constructor.newInstance(anInteger, "aString");

It would probably help if every instance of YourClassName implemented the same interface or extended the same base class so that you could cast the return value of newInstance() to that interface or base class. Then you would be able to call the interface or base class's methods on the instance. Otherwise, your only way to call useful methods on the Object would be to use reflection.

这篇关于如何使用变量名运行.java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆