Java基礎Demo -- 泛型上界的示例
阿新 • • 發佈:2018-12-24
<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); } }