X.M*_*.M. 7 java generics inheritance interface
设计简单,代码简单但不编译:
public interface Insertable {
public String getInsertStmt(String table);
}
public class ClassCanBeInserted implements Insertable {
public String getInsertStmt(String table) {}
}
public class SomeClass {
public static void main() {
List<ClassCanBeInserted> list = new ArrayList<ClassCanBeInserted>();
SomeMethod(list);
}
private static void SomeMethod(List<Insertable> list) {}
}
Run Code Online (Sandbox Code Playgroud)
并且对SomeMethod()的调用将无法编译.英语很差,但代码应该解释.有人可以找出什么是错的,以及如何获得一个设计的建议,可以在这种方法中使用接口实现的类列表?
再说一次,英国人很穷,所以可能表达不好.让我知道你的问题.谢谢!
给出的错误消息:类ClassCanBeInserted中的方法SomeMethod(List)不适用于参数(List)
Jor*_*rdi 15
List<Insertable>不是超类List<ClassCanBeInserted>,即使Insertable是超类ClassCanBeInserted.要执行您想要的操作,您应该将SomeMethod签名更改为SomeMethod(List<? extends Insertable> list).
| 归档时间: |
|
| 查看次数: |
1688 次 |
| 最近记录: |