Java Tutorial/Generics/Generic Class Hierarchies — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:20, 1 июня 2010
Содержание
- 1 A Generic Subclass: a nongeneric class can be the superclass of a generic subclass
- 2 A subclass can add its own type parameters, if needed.
- 3 Casting within a generic class hierarchy
- 4 Generic Class Hierarchies: uses a generic superclass
- 5 Overriding Methods in a Generic Class
- 6 Run-Time Type Comparisons Within a Generic Hierarchy
A Generic Subclass: a nongeneric class can be the superclass of a generic subclass
A nongeneric class can be the superclass of a generic subclass.
<source lang="java">
class NonGen {
int num; NonGen(int i) { num = i; } int getNumber() { return num; }
} class Gen<T> extends NonGen {
T ob; Gen(T o, int i) { super(i); ob = o; } T getObject() { return ob; }
}
public class MainClass {
public static void main(String args[]) { Gen<String> w = new Gen<String>("Hello", 47); System.out.print(w.getObject() + " "); System.out.println(w.getNumber()); }
}</source>
Hello 47
A subclass can add its own type parameters, if needed.
<source lang="java">
class Gen<T> {
T ob; Gen(T o) { ob = o; } T getob() { return ob; }
}
class Gen2<T, V> extends Gen<T> {
V ob2; Gen2(T o, V o2) { super(o); ob2 = o2; } V getob2() { return ob2; }
} public class MainClass {
public static void main(String args[]) { Gen2<String, Integer> x = new Gen2<String, Integer>("Value is: ", 99); System.out.print(x.getob()); System.out.println(x.getob2()); }
}</source>
Value is: 99
Casting within a generic class hierarchy
You can cast one instance of a generic class into another only if the two are otherwise compatible and their type arguments are the same.
<source lang="java">
class Gen<T> {
T ob; Gen(T o) { ob = o; } T getObject() { return ob; }
}
class Gen2<T> extends Gen<T> {
Gen2(T o) { super(o); }
}
public class MainClass {
public static void main(String args[]) { Gen<Integer> intObject = new Gen<Integer>(88); Gen2<Long> longObject = new Gen2<Long>(99L); //longObject = (Gen2<Long>)intObject; }
}</source>
Generic Class Hierarchies: uses a generic superclass
In a generic hierarchy, any type arguments needed by a generic superclass must be passed up the hierarchy by all subclasses.
<source lang="java">
class Gen<T> {
T ob; Gen(T o) { ob = o; } T getob() { return ob; }
} class Gen2<T> extends Gen<T> {
Gen2(T o) { super(o); }
}</source>
Overriding Methods in a Generic Class
<source lang="java">
class Gen<T> {
T ob; Gen(T o) { ob = o; } T getObject() { System.out.println("Gen"s getObject(): " ); return ob; }
} class Gen2<T> extends Gen<T> {
Gen2(T o) { super(o); } T getObject() { System.out.println("Gen2"s getObject(): "); return ob; }
} public class MainClass{
public static void main(String[] arg){ Gen<Integer> intObject = new Gen<Integer>(88); Gen2<Long> longObject = new Gen2<Long>(99L); intObject.getObject(); longObject.getObject(); }
}</source>
Gen"s getObject(): Gen2"s getObject():
Run-Time Type Comparisons Within a Generic Hierarchy
The run-time type information operator instanceof: determines if an object is an instance of a class. The instanceof operator can be applied to objects of generic classes.
<source lang="java">
class Gen<T> {
T ob; Gen(T o) { ob = o; } T getObject() { return ob; }
}
class Gen2<T> extends Gen<T> {
Gen2(T o) { super(o); }
}
public class MainClass {
public static void main(String args[]) { Gen<Integer> iOb = new Gen<Integer>(88); Gen2<Integer> iOb2 = new Gen2<Integer>(99); Gen2<String> strOb2 = new Gen2<String>("Generics Test"); if(iOb2 instanceof Gen2<?>){ System.out.println("iOb2 is instance of Gen2"); } if(iOb2 instanceof Gen<?>){ System.out.println("iOb2 is instance of Gen"); } if(strOb2 instanceof Gen2<?>){ System.out.println("strOb is instance of Gen2"); } if(strOb2 instanceof Gen<?>){ System.out.println("strOb is instance of Gen"); } if(iOb instanceof Gen2<?>){ System.out.println("iOb is instance of Gen2"); } if(iOb instanceof Gen<?>){ System.out.println("iOb is instance of Gen"); } // The following can"t be compiled because // generic type info does not exist at runtime.
// if(iOb2 instanceof Gen2<Integer>) // System.out.println("iOb2 is instance of Gen2<Integer>");
}
}</source>
iOb2 is instance of Gen2 iOb2 is instance of Gen strOb is instance of Gen2 strOb is instance of Gen iOb is instance of Gen