具有未知泛型接口类型的Java泛型编程
我使用了几个泛型类型的接口。当将它们组合在一起时,当我不得不从不知道泛型参数的具体类型的代码部分使用它们时,我会遇到一些问题 假设我有以下接口:具有未知泛型接口类型的Java泛型编程,java,generics,interface,raw-types,Java,Generics,Interface,Raw Types,我使用了几个泛型类型的接口。当将它们组合在一起时,当我不得不从不知道泛型参数的具体类型的代码部分使用它们时,我会遇到一些问题 假设我有以下接口: public interface MyObjectInterface<T extends Number> {} 公共接口MyObjectInterface{} 实现该interfaceare的对象存储在具有相同泛型类型的泛型集合中: public interface MyCollectioninterface<T extends
public interface MyObjectInterface<T extends Number> {}
公共接口MyObjectInterface{}
实现该interfaceare的对象存储在具有相同泛型类型的泛型集合中:
public interface MyCollectioninterface<T extends Number> {
public void updateObject(MyObjectInterface<T> o);
}
公共接口MyCollectioninterface{
public void updateObject(MyObjectInterface o);
}
MyCollectionInterface的具体实例包含相同泛型参数的多个MyObjectInterface:
public class ConcreteCollection<T extends Number> implements
MyCollectionInterface<T> {
List<MyObjectInterface<T>> list;
public void updateObject(MyObjectInterface<T> o){}
}
公共类集合实现
MyCollectionInterface{
名单;
public void updateObject(MyObjectInterface o){}
}
现在,我有几个问题,关于如何从一个
(而且必须)不知道泛型的具体类型
假设我有以下课程:
public class ClientClass{
private MyCollectionInterface<?> collection; //1st possibility
private MyCollectionInterface collection; //2nd possibility
public ClientClass(MyCollectionInterface<?> collection){
this.collection = collection;
}
public void foo(MyObjectInterface<?> o){
this.collection.updateObject(o); //this doesn't compile
}
public void foo(MyObjectInterface<? extends Number> o){
this.collection.updateObject(o); //this doesn't compile either
}
public void bar(MyObjectInterface o){
MyObject b = o; //warning
this.collection.updateObject(o); //this compile but with warnings
}
}
public类ClientClass{
私有MyCollectionInterface集合;//第一种可能
私有MyCollectionInterface集合;//第二种可能性
公共客户端类(MyCollectionInterface集合){
this.collection=collection;
}
公共void foo(MyObjectInterface o){
this.collection.updateObject(o);//这不会编译
}
public void foo(MyObjectInterface好的,我对您的代码进行了一些研究,得出了一个结论
问题是ConcreteCollection
(及其接口MyCollectionInterface
)将方法updateObject
声明为接收类型为MyObjectInterface
(其中T扩展了Number
)的参数-请注意,该类型是一个具体的类型(不是通配符)
现在,在您的客户机类中,您正在接收一个集合并将其存储为MyCollectionInterface
,但是传递给ClientClass
”构造函数的实例将是一种具体类型,例如:
new ClientClass(new ConcreteCollection<Integer>());
但是,您限制了可以在构造函数中接收的集合(这可能不是一个坏主意),或者:
2)修改界面以接受通配符类型:
public interface MyCollectionInterface<T extends Number> {
public void updateObject(MyObjectInterface<? extends Number> o);
}
public class ConcreteCollection<T extends Number> implements MyCollectionInterface<T> {
List<MyObjectInterface<T>> list;
public void updateObject(MyObjectInterface<? extends Number> o) {}
}
private MyCollectionInterface<?> collection;
public ClientClass(MyCollectionInterface<?> collection){
this.collection = collection;
}
public void foo(MyObjectInterface<?> o){
this.collection.updateObject(o); //compiles
}
在这种情况下,您还可以从MyCollectionInterface
和ConcreteCollection
中删除
,因为T
已不再使用
@最后的问题:
1) 可能是的
2) 你应该
3) 你不能,如果你真的不关心你在集合中存储了哪些对象,你应该完全抛弃泛型
很抱歉回答得太长,希望这有帮助。在方法foo
和bar
中,它不应该是MyObjectInterface
而不是MyObjectInterface
?是的,我的错。我修复了它。它是MyObjectInterface。问题不是方法foo。即使我更改它的签名,问题就在这里:public void updateObject(MyObjectInterface o){}。即使T声明为T扩展了数字,行:this.collection.updateObject(o);也不起作用。我可以声明o为偶数
public interface MyCollectionInterface<T extends Number> {
public void updateObject(MyObjectInterface<? extends Number> o);
}
public class ConcreteCollection<T extends Number> implements MyCollectionInterface<T> {
List<MyObjectInterface<T>> list;
public void updateObject(MyObjectInterface<? extends Number> o) {}
}
private MyCollectionInterface<?> collection;
public ClientClass(MyCollectionInterface<?> collection){
this.collection = collection;
}
public void foo(MyObjectInterface<?> o){
this.collection.updateObject(o); //compiles
}
List<MyObjectInterface<? extends Number>> list = new ArrayList<MyObjectInterface<? extends Number>>();