尝试添加带有方法的基接口(interface),以便所有派生类都必须实现该方法或使用默认方法。使该方法可调用的最佳方法是什么?请参阅下面代码块中的注释。
public interface IA{}
public interface IB{
public Integer doWork();
}
public interface IC extends IB{
}
class B implements IB{
Integer doWork(){
return 2;
}
}
class C extends B implements IC{
@Override
Integer doWork(){
return 7;
}
}
//What do I need to do to cast clazz to an object so I can call the derived class' doWork method?
private Integer newClient(Class<T> clazz){
((B) clazz).doWork();
}
最佳答案
最终找到了解决方案:
B.class.cast(clazz);
关于java - 在派生类的泛型对象上调用基方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52616434/