[英]String with the class name and get the Class.class
I've a string containing the class name but I want to get the Xyz.class 我有一个包含类名称的字符串,但我想获取Xyz.class
Like this: 像这样:
String className = "Xyz"
public Class getTheClass(className){
(what do I need to do here?)
return Xyz.class;
}
I know it has to do with reflection but it is not working the way I've been trying to do which is like this: 我知道它与反射有关,但是它不像我一直尝试的那样工作,就像这样:
public Class getObjectClass(String className) throws Exception
{
Class c = Class.forName(className);
return c.getClass();
}
But I'm getting: 但我得到:
java.lang.Class
And I want Xyz.class
我想要
Xyz.class
You don't need to call getClass()
here, because c
is already a Class
. 您无需在此处调用
getClass()
,因为c
已经是一个Class
。
public Class getObjectClass(String className) throws Exception {
Class c = Class.forName(className);
return c;
}
If you use getClass()
, then you're getting the class of a Class
object, which is, of course, java.lang.Class
. 如果使用
getClass()
,那么您将获得Class
对象的Class
,当然是java.lang.Class
。
Use: 采用:
return c;
Instead of 代替
return c.getClass();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.