我有一个接受单个参数的方法。该参数可以是任何类 A、B、C 或 D,它们都继承自同一父类 E。但是,我不知道父类的名称(也许是“E”,也许是“F”)。
我希望我的方法接受父类作为参数,这样我就可以传入一个子对象(因此我只需要该方法的一个实现)。
如何找到父类的名称?它位于一个 jar 中,我无法访问源代码。
编辑
方法示例:
private void processResults(final ???parentClassName??? aParentClassName) {
...
}
最佳答案
Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.
If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned.
If this object represents an array class then the Class object representing the Object class is returned.
示例代码:
A.class.getSuperclass().getName()
或
aObject.getClass().getSuperclass().getName()
关于java - 从子类获取父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24743836/