泛型2
7.泛型类的继承——以泛型类为父类
例7. 继承泛型类示例。
package fanxing; public class superGen<T> { T ob; public superGen(T ob){ this.ob = ob; } public superGen(){ ob = null; } public T getOb(){ return ob; } }
package fanxing; public class derivedGen<T> extends superGen<T> { public derivedGen(T ob){ super(ob); } }
复杂一点儿的子类如下:
package fanxing; //public class derivedGen<T> extends superGen<T> { // public derivedGen(T ob){ // super(ob); // } //} public class derivedGen<T,U> extends superGen<T> { U dob; public derivedGen(T ob, U dob){ super(ob); this.dob = dob; } public U getDob(){ return dob; } }
测试程序如下:
package fanxing; public class demoHerit_1 { public static void main(String[] args) { derivedGen<Integer,String> dg = new derivedGen<Integer,String>(100,"Value is:"); System.out.print(dg.getDob()); System.out.println(dg.getOb()); } }
程序的输出结果如下:
Value is:100
8. 泛型类的继承——以非泛型类为父类
例8. 继承非泛型类示例。
父类:
package fanxing; public class nonGen { int n; public nonGen(int n){ this.n = n; } public nonGen(){ n = 0; } public int getn(){ return n; } }
派生类:
package fanxing; public class derivednonGen<T> extends nonGen { T ob; public derivednonGen(T ob, int n){ super(n); this.ob = ob; } public T getOb(){ return ob; } }
测试程序:
package fanxing; public class demoHerit_2 { public static void main(String[] args) { derivednonGen<String> dng = new derivednonGen<String>("Value is:", 100); System.out.print(dng.getOb()); System.out.println(dng.getn()); } }
程序运行结果如下:
Value is:100
9. 运行时类型识别
例9. 泛型类的类型识别示例1。
package fanxing; public class demoRTTI_1 { public static void main(String[] args) { Generic<Integer> iob = new Generic<Integer>(100); Generic<String> sob = new Generic<String>("Good"); if(iob instanceof Generic) System.out.println("Generic<Integer> object is instance of Generic"); if(iob instanceof Generic<?>) System.out.println("Generic<Integer> object is instance of Generic<?>"); if(iob.getClass()==sob.getClass()) System.out.println("Generic<Integer> class equals Generic<String> class"); } }
程序输出结果为:
Generic<Integer> object is instance of Generic Generic<Integer> object is instance of Generic<?> Generic<Integer> class equals Generic<String> class
例10. 泛型类的类型识别示例2。
package fanxing; public class demoRTTI_2 { public static void main(String[] args) { superGen<Integer> sg = new superGen<Integer>(100); derivedGen<Integer, String> dg = new derivedGen<Integer, String>(100,"Good"); if(sg instanceof derivedGen) System.out.println("superGen object is instance of derivedGen!"); if(dg instanceof superGen) System.out.println("derivedGen object is instance og superGen!"); if(sg.getClass()==dg.getClass()) System.out.println("superGen class equals derivedGen class!"); } }
程序输出结果为:
derivedGen object is instance og superGen!
从上述结果可以看出,只有子类对象被instanceof识别为父类对象。
10.强制类型转换
泛型类的强制类型转换的一般格式如下:
(泛型类名<实际参数>)泛型对象
例11. 强制类型转换示例。
package fanxing; public class demoForceChange { public static void main(String[] args) { superGen<Integer> sg = new superGen<Integer>(100); derivedGen<Integer, String> dg = new derivedGen<Integer, String>(100,"Good"); if((superGen<Integer>)dg instanceof superGen) System.out.println("derivedGen object is changed to superGen"); if((derivedGen<Integer,String>)sg instanceof derivedGen) System.out.println("superGen object is changed to derivedGen"); } }
程序的运行结果如下:
derivedGen object is changed to superGen Exception in thread "main" java.lang.ClassCastException: fanxing.superGen cannot be cast to fanxing.derivedGen at fanxing.demoForceChange.main(demoForceChange.java:10)