爪哇。我如何使用反射向上转换类型?
我有两个类 UpObj 和 DownObj
public class UpObj {
public UpObj() {
System.out.println("Load UpObj ");
}
}
public class DownObj extends UpObj {
public DownObj() {
System.out.println("Load DownObj ");
}
}
public class Caller {
UpObj obj;
public Caller(UpObj obj) {
this.obj = obj;
System.out.println("!!!");
}
}
public class GNUMakeFile {
/**
* @param args
*/
public static void main(String[] args) {
DownObj iView = new DownObj();
Class<?> iViewClass = iView.getClass();
Class<?> clazz;
try {
clazz = Class.forName("bla.bla.bla.Caller");
Constructor<?> ctor = clazz.getDeclaredConstructor(iViewClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
所以,我想在调用者构造函数中将我的子类型 DownObj 向上转换为父 UpObj 。 我认为通过帮助泛型这是可能的。像这样的东西。 任何人都知道这个具体如何使用。
谢谢。
I have a two classes UpObj and DownObj
public class UpObj {
public UpObj() {
System.out.println("Load UpObj ");
}
}
public class DownObj extends UpObj {
public DownObj() {
System.out.println("Load DownObj ");
}
}
public class Caller {
UpObj obj;
public Caller(UpObj obj) {
this.obj = obj;
System.out.println("!!!");
}
}
public class GNUMakeFile {
/**
* @param args
*/
public static void main(String[] args) {
DownObj iView = new DownObj();
Class<?> iViewClass = iView.getClass();
Class<?> clazz;
try {
clazz = Class.forName("bla.bla.bla.Caller");
Constructor<?> ctor = clazz.getDeclaredConstructor(iViewClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
So, I want upcast my child type DownObj to parent UpObj in Caller constructor.
I think this is possible with help generics. Something like this .
Anybody know how exactly this use.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您实际上不应该将 Class.getConstructor() 与要传递给它的运行时类型一起使用 - 该方法根据形式参数进行工作。
如果您需要搜索与您拥有的某些对象匹配的构造函数,则必须循环
getConstructors()
,检查每个构造函数的形参,检查是否找到了合适的使用 <代码>Class.isAssignableFrom()。我认为没有一种方便的方法可以让反射 API 为您执行重载解析。(或者,重新考虑你的方法,这样就不再需要搞乱反射了。)
You shouldn't really be using
Class.getConstructor()
with the runtime type of what you want to pass to it – the method works off the formal parameters.If you need to search for a constructor that matches some objects that you have, you'll have to loop over
getConstructors()
, inspect the formal parameters of each constructor, and check whether you found a suitable signature usingClass.isAssignableFrom()
. I don't think there's a convenient way to have the reflection API do overload resolution for you.(Alternatively, rethink your approach so messing with reflection is no longer necessary.)
没有标准的方法来做到这一点。尝试在谷歌上搜索一些解决方案,例如 http://www.xinotes.org/notes/note /1329/ 或 如何使用获取参数类型反射?。我记得 Spring 和其他一些库有类似的 util 函数来做到这一点。你用春天吗?
There is no standard way doing it. Try to google for some solutions, e.g. http://www.xinotes.org/notes/note/1329/ or How to get parameter types using reflection? . As I remember the Spring and some other libraries have similar util functions to do it. Do you use the Spring?
我不太明白这个问题 - 通常不需要向上转换。
但问题似乎是获取对象的上层类,
如果使用 getSuperclass 方法:
文档:getSuperclass()
I didn't really understand the problem - upcasting is normally not needed.
but it seams like the problem is to get the upper class of the object,
if use the
getSuperclass
method:Documentation: getSuperclass()