我有一个类似于这样的方法
我知道我必须将其转换为类,但然后我必须对每个可能的类进行一个instanceof检查,然后再进行转换。
所以现在我有这样的东西:
public static <T extends MyClass, X extends AnotherClass> List<T> (Class<T> aParameter, X anotherParameter)
现在,如果AnotherClass是一个抽象类,它没有定义getId方法,但是每个继承该接口的类都有。 (不要问我为什么会这样设计,我没有设计这个抽象类,也不被允许更改它)。
那么我该如何做到这一点呢?
anotherParameter.getId();
我知道我必须将其转换为类,但然后我必须对每个可能的类进行一个instanceof检查,然后再进行转换。
所以现在我有这样的东西:
if (anotherParameter instanceof SomeClass)
((SomeClass)anotherParameter).getId(); //This looks bad.
在运行时,是否可以动态地将其转换为另一个参数(即anotherParameter)?