Java Tutorial/Generics/Generic Class Hierarchies

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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.



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());
  } 
}



Hello 47


A subclass can add its own type parameters, if needed.

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());
  }
}



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.



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;
 
  }  
}





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.



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);
  }
}





Overriding Methods in a Generic Class

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();
  }
  
}



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.



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>"); 
  }  
}



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