Java 泛型超级vs.扩展
就在我认为自己终于理解了泛型的时候,我遇到了以下示例:Java 泛型超级vs.扩展,java,generics,scjp,generic-type-argument,Java,Generics,Scjp,Generic Type Argument,就在我认为自己终于理解了泛型的时候,我遇到了以下示例: public class Organic<E> { void react(E e) { } static void main(String[] args) { //1: Organic<? extends Organic> compound = new Aliphatic<Organic>(); //2: Organ
public class Organic<E> {
void react(E e) { }
static void main(String[] args) {
//1: Organic<? extends Organic> compound = new Aliphatic<Organic>();
//2: Organic<? super Aliphatic> compound = new Aliphatic<Organic>();
compound.react(new Organic());
compound.react(new Aliphatic());
compound.react(new Hexane());
} }
class Aliphatic<F> extends Organic<F> { }
class Hexane<G> extends Aliphatic<G> { }
如果第2行是ucommented,则不会编译以下内容:
compound.react(new Organic());
在第二个例子中,脂肪族及其超型是允许的。那么为什么不允许使用脂肪族呢
在第一个例子中,为什么不允许新的有机物
第一个编译器错误:
- The method react(capture#1-of ? extends Organic) in the type Organic<capture#1-of ? extends Organic> is not applicable for the arguments (Organic)
- The method react(capture#2-of ? extends Organic) in the type Organic<capture#2-of ? extends Organic> is not applicable for the arguments (Aliphatic)
- The method react(capture#3-of ? extends Organic) in the type Organic<capture#3-of ? extends Organic> is not applicable for the arguments (Hexane)
- The method react(capture#1-of ? super Aliphatic) in the type Organic<capture#1-of ? super Aliphatic> is not applicable for the arguments (Organic)
-类型为Organic的react(capture#1-of?extends Organic)方法不适用于参数(Organic)
-有机物类型中的react(capture#2-of?extends Organic)方法不适用于参数(脂肪族)
-类型为Organic的react(capture#3-of?extends Organic)方法不适用于参数(己烷)
第二个编译器错误:
- The method react(capture#1-of ? extends Organic) in the type Organic<capture#1-of ? extends Organic> is not applicable for the arguments (Organic)
- The method react(capture#2-of ? extends Organic) in the type Organic<capture#2-of ? extends Organic> is not applicable for the arguments (Aliphatic)
- The method react(capture#3-of ? extends Organic) in the type Organic<capture#3-of ? extends Organic> is not applicable for the arguments (Hexane)
- The method react(capture#1-of ? super Aliphatic) in the type Organic<capture#1-of ? super Aliphatic> is not applicable for the arguments (Organic)
-有机物类型中的方法react(捕获1-超脂肪族)不适用于参数(有机)
你做了一些混合
有机
的类型与有机
相同
不能执行
new Organic())代码>
你必须至少考虑执行
<代码>新的有机());<代码>
这只是我能想到的一个编译错误
这当然对其他对象实例化有效
我认为您误解了在react()
方法中设置为参数的内容
试着改变
void react(E e) { }
到
void-react(有机e){
看看有什么不同。您正在查找对象:有机
Aliphetic
己烷
不作为E
NorAliphetic
asE
也不要将己烷作为您的第一次声明
Organic<? extends Organic> compound
Organic<? super Aliphatic> compound
这意味着compount
可能是有机的,这是这些仿制药的科学背景。)该主题问题也在其他几个地方讨论,如:
这实际上是上一次考试(问题34)中的一个问题。这本准备书是根据这本书编写的
即使在这里和其他链接下的解释以及书中的文字,我也不清楚解决方案,因为解释大多基于列表界面,阅读时我认为这是一些特定于内部收藏的解决方案
但是,如果您看到列表接口和添加-方法的定义,以及有机类及其反应-方法的定义,您会注意到它们的定义方式类似
public interface List<E> extends Collection<E> {
...
boolean add(E e);
...
}
public class Organic<E> {
...
void react(E e) { }
...
}
公共接口列表扩展集合{
...
布尔加法(E);
...
}
公共类有机食品{
...
空位反应(E){}
...
}
因此,所有基于列表接口示例的解释,你可以在任何地方找到,对这个问题也是有效的
List<? extends String> list1 = new ArrayList<String>();
List<? super String> list2 = new ArrayList<String>();
list1.add(new String()); //The method add(capture#1-of ? extends String) in the type List<capture#1-of ? extends String> is not applicable for the arguments (String) - // compile-time error
list2.add(new Object()); //The method add(capture#2-of ? super String) in the type List<capture#2-of ? super String> is not applicable for the arguments (Object) - // compile-time error
list您自己尝试过这个方法吗?您遇到了什么编译器错误?我知道我遇到了什么,我不明白为什么。可能是重复的