简单的设计和简单的代码,但不编译:
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) {}
}
并且对SomeMethod()的调用将无法编译。英语差,但代码应说明。有人可以找出问题所在吗,以及如何为提议在该方法中使用接口(interface)实现的类的列表进行设计吗?
再次,英语差,所以可能表示不好。让我知道你的问题。谢谢!
给出错误信息:
类型ClassCanBeInserted的方法SomeMethod(List)不适用于参数(List)
最佳答案
List<Insertable>
不是List<ClassCanBeInserted>
的父类(super class),即使Insertable
是ClassCanBeInserted
的父类(super class)也是如此。若要执行所需的操作,应将SomeMethod签名更改为SomeMethod(List<? extends Insertable> list)
。