[英]How to override a generic method
Generic method : 通用方法:
public <T> void foo(T t);
Desired overridden method : 所需的覆盖方法:
public void foo(MyType t);
What is the java syntax to achieve this? 实现此目的的Java语法是什么?
You might want to do something like this : 您可能想做这样的事情:
abstract class Parent {
public abstract <T extends Object> void foo(T t);
}
public class Implementor extends Parent {
@Override
public <MyType> void foo(MyType t) {
}
}
A similar question was answered here as well : Java generic method inheritance and override rules 这里也回答了类似的问题: Java通用方法继承和覆盖规则
A better design is. 更好的设计是。
interface Generic<T> {
void foo(T t);
}
class Impl implements Generic<MyType> {
@Override
public void foo(MyType t) { }
}
interface Base {
public <T> void foo(T t);
}
class Derived implements Base {
public <T> void foo(T t){
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.