Java/Generics/Generic Method

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

Demonstrate a simple generic method.

/*
Java 2, v5.0 (Tiger) New Features
by Herbert Schildt
ISBN: 0072258543
Publisher: McGraw-Hill/Osborne, 2004
*/
 
public class GenMethDemo {  
 
  // Determine if an object is in an array. 
  static <T, V extends T> boolean isIn(T x, V[] y) { 
 
    for(int i=0; i < y.length; i++) 
      if(x.equals(y[i])) return true; 
 
    return false; 
  } 
 
  public static void main(String args[]) {  
 
    // Use isIn() on Integers. 
    Integer nums[] = { 1, 2, 3, 4, 5 }; 
 
    if(isIn(2, nums)) 
      System.out.println("2 is in nums"); 
 
    if(!isIn(7, nums)) 
      System.out.println("7 is not in nums"); 
 
    System.out.println(); 
 
    // Use isIn() on Strings. 
    String strs[] = { "one", "two", "three", 
                      "four", "five" }; 
 
    if(isIn("two", strs)) 
      System.out.println("two is in strs"); 
 
    if(!isIn("seven", strs)) 
      System.out.println("seven is not in strs"); 
 
    // Opps! Won"t compile! Types must be compatible. 
//    if(isIn("two", nums)) 
//      System.out.println("two is in strs"); 
  }  
}





Java generic: Ambiguity caused by erasure on overloaded methods.

/*
Java 2, v5.0 (Tiger) New Features
by Herbert Schildt
ISBN: 0072258543
Publisher: McGraw-Hill/Osborne, 2004
*/
class MyGenClass<T, V> {  
  T ob1;  
  V ob2;  
 
  // ... 
 
  // These two overloaded methods are ambiguous.  
  // and will not compile. 
  void set(T o) { 
    ob1 = o; 
  } 
 
  void set(V o) { 
    ob2 = o; 
  } 
}
// Can"t create an instance of T. 
class Gen<T> {  
  T ob;  
  Gen() {  
    ob = new T(); // Illegal!!! 
  }  
} 
public class Wrong<T> {  
  // Wrong, no static variables of type T. 
  static T ob; 
    
  // Wrong, no static method can use T. 
  static T getob() { 
    return ob; 
  } 
 
  // Wrong, no static method can access object 
  // of type T. 
  static void showob() { 
    System.out.println(ob); 
 } 
}





Java generic: A situation that creates a bridge method.

/*
Java 2, v5.0 (Tiger) New Features
by Herbert Schildt
ISBN: 0072258543
Publisher: McGraw-Hill/Osborne, 2004
*/
//  
class Gen<T> {  
  T ob; // declare an object of type T  
    
  // Pass the constructor a reference to   
  // an object of type T.  
  Gen(T o) {  
    ob = o;  
  }  
  
  // Return ob.  
  T getob() {  
    return ob;  
  }  
}  
 
// A subclass of Gen. 
class Gen2 extends Gen<String> { 
 
  Gen2(String o) { 
    super(o); 
  } 
 
  // A String-specific override of getob(). 
  String getob() { 
    System.out.print("You called String getob(): "); 
    return ob; 
  } 
} 
  
// Demonstrate a situation that requires a bridge method. 
public class BridgeDemo {  
  public static void main(String args[]) {  
 
    // Create a Gen2 object for Strings. 
    Gen2 strOb2 = new Gen2("Generics Test");  
 
    System.out.println(strOb2.getob()); 
  }  
}





Overriding a generic method in a generic class.

/*
Java 2, v5.0 (Tiger) New Features
by Herbert Schildt
ISBN: 0072258543
Publisher: McGraw-Hill/Osborne, 2004
*/
class Gen<T> {  
  T ob; // declare an object of type T  
    
  // Pass the constructor a reference to   
  // an object of type T.  
  Gen(T o) {  
    ob = o;  
  }  
  
  // Return ob.  
  T getob() {  
    System.out.print("Gen"s getob(): " ); 
    return ob;  
  }  
}  
 
// A subclass of Gen that overrides getob(). 
class Gen2<T> extends Gen<T> { 
 
  Gen2(T o) { 
    super(o); 
  } 
   
  // Override getob(). 
  T getob() {  
    System.out.print("Gen2"s getob(): "); 
    return ob;  
  }  
} 
  
// Demonstrate generic method override. 
public class OverrideDemo {  
  public static void main(String args[]) {  
    
    // Create a Gen object for Integers. 
    Gen<Integer> iOb = new Gen<Integer>(88); 
 
    // Create a Gen2 object for Integers. 
    Gen2<Integer> iOb2 = new Gen2<Integer>(99);  
   
    // Create a Gen2 object for Strings. 
    Gen2<String> strOb2 = new Gen2<String>("Generics Test");  
 
    System.out.println(iOb.getob()); 
    System.out.println(iOb2.getob()); 
    System.out.println(strOb2.getob()); 
  }  
}





Return generic value from method

import java.io.Serializable;
class Base {
}
class Sub1 extends Base implements Serializable {
  public void run() {
  }
}
class Sub2 extends Base implements Serializable {
  public void run() {
  }
}
public class TypeInference {
  static <T extends Base> T infer(T t1, T t2) {
    return null;
  }
  public static void main(String[] args) {
    Base base = infer(new Sub1(), new Sub2());
    Serializable runnable = infer(new Sub1(), new Sub2());
  }
}