Fork me on GitHub

泛型

package fanxing;

import java.util.ArrayList;
import java.util.Collection;

public class testdemo {
public static void main(String[] args) {
	//同一类型
	Collection<String> a=new ArrayList<String>();
	Collection<father> b=new ArrayList<father>();
//	Collection<father> b=new ArrayList<son>();error
	Collection<son> c=new ArrayList<son>();
	//<? extends E>向下限定,E及其子类
	Collection<? extends Object> d=new ArrayList<Object>();
	Collection<? extends Object> e=new ArrayList<father>();
	Collection<? extends Object> f=new ArrayList<son>();
	Collection<? extends father> g=new ArrayList<son>();
	//Collection<? extends father> h=new ArrayList<Object>();error
	//<?>表示任意类型 Object或者其子类
	Collection<?> a1=new ArrayList<son>();
	Collection<?> a2=new ArrayList<father>();
	Collection<?> a3=new ArrayList<Object>();
	
	//<? super E>向上限定 E及其父类
	Collection<? super Object> b1=new ArrayList<Object>();
	//Collection<? super Object> b2=new ArrayList<father>();error
	//Collection<? super Object> b3=new ArrayList<son>();error
	//Collection<? super father> b4=new ArrayList<son>();error
	Collection<? super son> b2=new ArrayList<Object>();
	Collection<? super father> b3=new ArrayList<Object>();
	Collection<? super son> b4=new ArrayList<father>();
	
}

	
	
}
class father{
	public void show(){
		System.out.println("sss");
	}
}
class son extends father{
	public void show(){
		System.out.println("son");
	}
}

  

posted @ 2018-08-31 21:23  雪人奥特曼  阅读(121)  评论(0编辑  收藏  举报