Java基础Demo -- 泛型上界的示例

<T extends SuperClass> 泛型上界的定义

<? extends SuperClass> 有界通配符的运用

普通泛型方法的运用

静态泛型方法的运用

class Grandpa
{
	private int x,y;
	public Grandpa(int x, int y){
		this.x = x;
		this.y = y;
	}
	
	public int getX(){ return this.x; }
	public int getY(){ return this.y; }
 
	public void grandpaMtd(){
		System.out.println("grandpaMethod::[x="+x+",y="+y+"]\r\n");
	}
 
	protected void common(){
		System.out.println("i am "+this.getClass().getName()+", 调用超级类中的common()方法!\r\n");
	}

	public String toString(){
		return "i am "+this.getClass().getName()+"[x"+x+",y"+y+"] printed....\r\n" ;
	}
}
 
class Father extends Grandpa
{
	private int x,y,z;
	public Father(int x, int y, int z){
		super(x,y); //父类的x,y是私有变量,会被继承下来,但是子类无法访问.这里的super(x,y)只是初始化了父类中的x,y并没有初始化本类自己的x,y成员
		this.x = x;
		this.y = y;
		this.z = z;
	}
 
	public int getX(){ return this.x; }
	public int getY(){ return this.y; }
	public int getZ(){ return this.z; }
 
	public void fatherMtd(){
 
		String superClassName = Father.class.getSuperclass().getName();
		String grandpaXY = superClassName+"[x="+super.getX()+",y="+super.getY()+"]";
 
		String currentClassName = Father.class.getName();
		String fatherXYZ = currentClassName+"[x="+x+",y="+y+",z="+z+"]\r\n";
 
		System.out.println("fatherMethod::"+grandpaXY+"--"+fatherXYZ);
	}
}
 
class Son extends Father
{
	private int x,y,z,m;
	public Son(int x, int y, int z, int m){
		super(x,y,z);
		this.x = x;
		this.y = y;
		this.z = z;
		this.m = m;
	}
 
	public void sonMtd(){
 
		String superClassName = Son.class.getSuperclass().getName();
		String fatherXYZ = superClassName+"[x="+super.getX()+",y="+super.getY()+",z="+super.getZ()+"]";
 
		String currentClassName = Son.class.getName();
		String sonXYZM = currentClassName+"[x="+x+",y="+y+",z="+z+",m="+m+"]\r\n";
 
		System.out.println("sonMethod::"+fatherXYZ+"--"+sonXYZM);
	}
}
 
/**
* 泛型类:限定了本类处理的T类型必须是Grandpa或者它的子类
*/
class MyCls<T extends Grandpa>
{
	private T t;
	public MyCls(){}
	public MyCls(T t){ this.t = t; }
 
	public void setValue(T t){ this.t = t; }
	public T getValue(){ return t; }

	public <T> void printT(T t){
		String msg = ((Grandpa)t).toString();
		System.out.println(msg);
	}
}
 
 
public class GenericOfExtendsSuperClassDemo 
{
	public static void aaaMethod( MyCls<?> arg ){//静态方法:通配符类型参数
		if( arg.getValue() instanceof Son ){ ((Son)arg.getValue()).sonMtd(); }
		else if( arg.getValue() instanceof Father){ ((Father)arg.getValue()).fatherMtd(); }
		else if( arg.getValue() instanceof Grandpa){ ((Grandpa)arg.getValue()).grandpaMtd(); }
	}
	
	public static void bbbMethod( MyCls<? extends Father> arg ){//静态方法:有上界的通配符类型参数
		System.out.print("current object classType is: "+arg.getValue().getClass().getName()+", invoke ");
		arg.getValue().fatherMtd(); 
	}
 
	public static void cccMethod( MyCls<Son> arg ){//静态方法:明确的类型参数
		System.out.print("I am fixed MyCls<Son>, only deal arg's Type is MyCls<Son> : "+arg.getValue().getClass().getName()+", invoke ");
		arg.getValue().sonMtd(); 
	}
 
	public static <T> void callCommon(T t){//静态泛型方法
		if(t instanceof Grandpa){
			((Grandpa)t).common(); //t的类型是T, T限制了上界<T extends Grandpa>, 所以这里的t可以调用超类Grandpa中的commonSuper()超类方法
		}		
	};
 
	//public static void cccMethod( MyCls<T> arg ){} //无法编译! 
	//泛型<T>定义: 
        //          结合泛型类上的<T>,可以用做普通方法的参数定义(泛型类上有<T>,上下文环境)
        //          不可以做静态方法的参数定义
	//泛型<T>使用:
	//            需要明确T的类型 (比如: MyCls<Son>) 
	//       或者 使用?通配符 (比如: MyCls<?>) 
	//       或者 使用?有界通配符 (比如: MyCls<? extends Father>) 
	//       或者 仅仅用来做obj的类型强制转换 (比如:(T)obj; )
 
	public static void main(String[] args) 
	{
		MyCls<Grandpa> mG = new MyCls<>();
		mG.setValue(new Grandpa(1,2));
 
		MyCls<Father> mF = new MyCls<>();
		mF.setValue(new Father(3,4,5));
 
		MyCls<Son> mS = new MyCls<>();
		mS.setValue(new Son(6,7,8,9));
 
		callCommon(new Grandpa(11,22));	  //静态泛型方法的运用
		callCommon(new Father(33,44,55));
		callCommon(new Son(66,77,88,99));

		mG.printT(new Grandpa(111,222));//普通泛型方法的运用
		mF.printT(new Father(333,444,555));
		mS.printT(new Son(666,777,888,999));
 
		
		// 泛型类的定义 class MyCls<T extends Grandpa>{}限定泛型上界, T只能是Grandpa或者Grandpa的子类
 
		aaaMethod(mG); //aaaMethod(MyCls<?> arg)方法:问号通配 MyCls<Grandpa> MyCls<Father> MyCls<Son>
		aaaMethod(mF);
		aaaMethod(mS);
 
		//bbbMethod(mF); //无法编译!aaaMethod(MyCls<? extends Father> arg)方法:有上界的问号只能通配 MyCls<Father> MyCls<Son>
		bbbMethod(mF);
		bbbMethod(mS);
 
		//cccMethod(mF); //无法编译!aaaMethod(MyCls<Son> arg)方法:只能接受类型为MyCls<Son>的参数
		//cccMethod(mF); //无法编译!
		cccMethod(mS);
	}
}

猜你喜欢

转载自blog.csdn.net/mmlz00/article/details/85181123
今日推荐