一个类的所有父类 [英] All parent classes of a class

查看:97
本文介绍了一个类的所有父类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能找到一个类的所有父类的列表,比如class1。

我想要一个用Java编码的这个类class1的所有父类的列表。

有什么方法???

解决方案

你需要使用Java 反射

http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29#Java [ ^ ]。



您需要使用: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getSuperclass%28%29 [ ^ ]。



如果你真的是指课程(不是interfac es或泛类),请记住,在Java中只支持单一继承(更准确地说,当你考虑接口时,一个类可以实现任意数量的接口;这种形式的多重继承有时被称为多重继承的弱形式)。所以,你需要进行链计算:为某个类找到一个超类,然后找到它的超类,一直向上到公共根类 java.lang.Object



有关所有相关细节,请参阅上面引用的整篇文章。







回答后续问题:

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html# isAssignableFrom%28java.lang.Class%29 [ ^ ]。



-SA

How could I find list of all parent classes of a class, say class1.
I want a list of all parent classes of this class class1 that I have coded in Java.
Is there any way???

解决方案

You need to use Java reflection:
http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29#Java[^].

You need to use: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getSuperclass%28%29[^].

If you really mean "classes" (not interfaces or generic classes), keep in mind that in Java only singular inheritance is supported (more exactly, when you take into account also interfaces, a class can implement any number of interfaces; this form of multiple inheritance sometimes is called weak form of multiple inheritance). So, you need to do the chain calculation: find a super class for some class, then its super class, all the way upward to the common root class java.lang.Object.

For all related detail, please see the whole article referenced above.

[EDIT]

In response to a follow-up question:
http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#isAssignableFrom%28java.lang.Class%29[^].

—SA


这篇关于一个类的所有父类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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