如何在运行时传递java类作为引用?
我想将一个类作为参数传递给函数。但我有包含该类名称的字符串变量。
I want to pass a class as an argument to a function. But I have the string variable which contains name of that class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一般方法,您需要使用完全限定的类名。
This is the general method, you'll need to use a fully qualified class name.
使用
Class
类的静态方法此方法返回您的类的
Class 类对象...
Use static method of
Class
classthis method return a
Class class object for your class...
尝试
Try
试试这个:
但相应的类必须在类路径中。
try this:
but the corresponding class must be in the classpath.
您可以使用反射:
You can use reflection: