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