第一种是固定的一种泛型,第二种是只要是Object类的子类都可以,换言之,任何类都可以,因为Object是所有类的根基类固定的泛型指类型是固定的,比如:Interge,String. 就是
<? extends Collection> 这里?代表一个未知的类型,但是,这个未知的类型实际上是Collection的一个子类,Collection是这个通配符的上限.举个例子class Test { }其中,限定了构造此类实例的时候T是一个确定类型(具体类型),这个类型实现了Collection接口,但是实现 Collection接口的类很多很多,如果针对每一种都要写出具体的子类类型,那也太麻烦了,干脆还不如用Object通用一下。 extends Collection>其中,?是一个未知类型,是一个通配符泛型,这个类型是实现Collection接口即可。_________________________上面讲的是什么鬼,当你知道引入通配符泛型的由来之后(下面代码由java1234.com提供)_________________________________________________________________________________________
The method take(Animal) in the type Test is not applicable for the arguments (Demo)The method take(Animal) in the type Test is not applicable for the arguments (Demo)The method take(Animal) in the type Test is not applicable for the arguments (Demo)
当引入泛型之后,遇到这种情况,参数怎么写都不适合,总有2个方法不适用,为了给泛型类写一个通用的方法,这时候就需要引入了 ?通配符的概念。
Demo Animal></span><span style="color:rgb(0,255);line-height:1.5 !important;">private</span><span style="line-height:1.5 !important;"> T ob;
</span><span style="color:rgb(0,255);line-height:1.5 !important;">public</span><span style="line-height:1.5 !important;"> T getOb() {
</span><span style="color:rgb(0,255);line-height:1.5 !important;">return</span><span style="line-height:1.5 !important;"> ob;
}
</span><span style="color:rgb(0,255);line-height:1.5 !important;">void</span><span style="line-height:1.5 !important;"> setOb(T ob) {
</span><span style="color:rgb(0,255);line-height:1.5 !important;">this</span>.ob =<span style="line-height:1.5 !important;"> ob;
}
</span><span style="color:rgb(0,255);line-height:1.5 !important;">public</span><span style="line-height:1.5 !important;"> Demo(T ob) {
</span><span style="color:rgb(0,255);line-height:1.5 !important;">super</span><span style="line-height:1.5 !important;">();
</span><span style="color:rgb(0,255);line-height:1.5 !important;">this</span>.ob =<span style="line-height:1.5 !important;"> ob;
}
</span><span style="color:rgb(0,255);line-height:1.5 !important;">void</span><span style="line-height:1.5 !important;"> print(){
System.out.println(</span>"T的类型是:"+<span style="line-height:1.5 !important;">ob.getClass().getName());
}
}