本文介绍了泛型中的通配符:“?超级T“同时工作“?延伸T“才不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的问题是关于Java 7中的泛型。假设我们有这样的类层次结构: interface Animal {} class Lion implements Animal {} class Butterfly implements Animal {} 就像 Java泛型教程 我们还有一个类 class Cage< T> {私人列表< T> arr = new ArrayList<>(); public void add(T t){ arr.add(t); } public T get(){ return arr.get(0); $ / code $ / pre 这里是使用这些类的代码: public static void main(String [] args){ Cage Animal a = cage.get(); // OK cage.add(new Lion()); //编译时错误 cage.add(new Butterfly()); //编译时错误} 问题1: 我已阅读此处这些问题,但只有像 Cage<?> 。但是我告诉编译器<?在 Cage< T> 中扩展Animal> ,所以键入 T 将会是任何子类型动物类型。那么为什么它仍然给编译时间错误? $ C>笼< ;?超级动物>笼子= ... 而不是笼子一切工作正常,编译器没有说任何不良。为什么在这种情况下它工作正常,而在上面的例子中它失败了?解决方案笼子必须能够容纳两种类型的动物。 超级说 - 它说笼子必须能够容纳所有类型的动物 - 也许还有其他一些东西,因为?超级动物可能是动物的超级类别。 extend表示,它可以容纳一些种动物 - 可能只是狮子,例如: 笼< ;?延伸动物>笼子=新笼子< Lion>(); 这将是一个有效的说法,但显然狮子笼不会抱蝴蝶, p> cage.add(new Butterfly()); 不会编译。声明 cage.add(new Lion()); 也不会编译,因为Java在这里查看笼子的声明 - 笼< ;? extends Animal> - 不是现在分配给它的对象( Cage< Lion> )。 我知道的泛型的最佳描述在 O'Reilly的Java in a Nutshell 。本章免费在线 - 第1部分和第2部分。 My question is about generics in Java 7. Suppose we have such class hierarchy:interface Animal {} class Lion implements Animal {} class Butterfly implements Animal {}Just like in Java Generics TutorialAlso we have a class class Cage<T> { private List<T> arr = new ArrayList<>(); public void add(T t) { arr.add(t); } public T get() { return arr.get(0); }}And here is the code which uses that classes:public static void main(String[] args) { Cage<? extends Animal> cage = new Cage<>(); Animal a = cage.get(); //OK cage.add(new Lion()); //Compile-time error cage.add(new Butterfly()); //Compile-time error }Question #1:I have read here about these issues but there was simply like Cage<?>. But I tell the compiler <? extends Animal> so type T in Cage<T> will be any of subtypes of Animal type. So why it still gives a compile time error?Question #2:If I specify Cage<? super Animal> cage = ... instead of Cage<? extends Animal> cage = ... everything works fine and compiler doesn't say anything bad. Why in this case it works fine while in the example above it fails? 解决方案 The cage must be able to hold both types of animals. "super" says that - it says that the Cage must be able to hold all types of animals - and maybe some other things, too, because ? super Animal might be a superclass of Animal. "extends" says that it can hold some kinds of animals - maybe just Lions, for instance, as in:Cage<? extends Animal> cage = new Cage<Lion>();which would be a valid statement, but obviously the lion cage won't hold butterflies, socage.add(new Butterfly()); wouldn't compile. The statementcage.add(new Lion());wouldn't compile either, because Java here is looking at the declaration of the cage - Cage<? extends Animal> - not the object that's assigned to it right now (Cage<Lion>).The best description of generics I know of is in O'Reilly's Java in a Nutshell. The chapter is free online - part 1 and part 2. 这篇关于泛型中的通配符:“?超级T“同时工作“?延伸T“才不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 22:34