class Holder<T> {
  private T value;
  public Holder() {}
  public Holder(T val) { value = val; }
  public void set(T val) { value = val; }
  public T get() { return value; }
  public boolean equals(Object obj) {
    return value.equals(obj);
  }
} /* Output: (Sample)
java.lang.ClassCastException: Apple cannot be cast to Orange
true
*///:~public class Wildcards {
  static <T> T wildSubtype(Holder<? extends T> holder, T arg) {
    // holder.set(arg); // Error:
    //   set(capture of ? extends T) in
    //   Holder<capture of ? extends T>
    //   cannot be applied to (T)
    T t = holder.get();
    return t;
  }
  static <T> void wildSupertype(Holder<? super T> holder, T arg) {
    holder.set(arg);
    // T t = holder.get();  // Error:
    //   Incompatible types: found Object, required T    // OK, but type information has been lost:
    Object obj = holder.get();
  }
  public static void main(String[] args) {    Holder<?> unbounded = new Holder<Long>();
    Long lng = 1L;

    // OK, but can only return Object:
    Object r11 = wildSubtype(unbounded, lng);    // wildSupertype(unbounded, lng); // Error:
    //   wildSupertype(Holder<? super T>,T) cannot be
    //   applied to (Holder<capture of ?>,Long)} ///:~wildSubtype  可以调用unbounded   为什么 wildSupertype调用unbounded出错?

解决方案 »

  1.   

    class Holder <T> { 
      private T value; 
      public Holder() {} 
      public Holder(T val) { value = val; } 
      public void set(T val) { value = val; } 
      public T get() { return value; } 
      public boolean equals(Object obj) { 
        return value.equals(obj); 
      } 
    } /* Output: (Sample) 
    java.lang.ClassCastException: Apple cannot be cast to Orange 
    true 
    *///:~ public class Wildcards { 
      static <T> T wildSubtype(Holder <? extends T> holder, T arg) { 
        // holder.set(arg); // Error: 
        //  set(capture of ? extends T) in 
        //  Holder <capture of ? extends T> 
        //  cannot be applied to (T) 
        T t = holder.get(); 
        return t; 
      } 
      static <T> void wildSupertype(Holder <? super T> holder, T arg) { 
        holder.set(arg); 
        // T t = holder.get();  // Error: 
        //  Incompatible types: found Object, required T     // OK, but type information has been lost: 
        Object obj = holder.get(); 
      } 
      public static void main(String[] args) {     Holder <?> unbounded = new Holder <Long>(); 
        Long lng = 1L;     // OK, but can only return Object: 
        Object r11 = wildSubtype(unbounded, lng);     // wildSupertype(unbounded, lng); // Error: 
        //  wildSupertype(Holder <? super T>,T) cannot be 
        //  applied to (Holder <capture of ?>,Long) } ///:~ 
      

  2.   


    class Holder <T> { 
      private T value; 
      public Holder() {} 
      public Holder(T val) { value = val; } 
      public void set(T val) { value = val; } 
      public T get() { return value; } 
      public boolean equals(Object obj) { 
        return value.equals(obj); 
      } 
    } /* Output: (Sample) 
    java.lang.ClassCastException: Apple cannot be cast to Orange 
    true 
    *///:~ public class Wildcards { 
      static <T> T wildSubtype(Holder <? extends T> holder, T arg) { 
        // holder.set(arg); // Error: 
        //  set(capture of ? extends T) in 
        //  Holder <capture of ? extends T> 
        //  cannot be applied to (T) 
        T t = holder.get(); 
        return t; 
      } 
      static <T> void wildSupertype(Holder <? super T> holder, T arg) { 
        holder.set(arg); 
        // T t = holder.get();  // Error: 
        //  Incompatible types: found Object, required T     // OK, but type information has been lost: 
        Object obj = holder.get(); 
      } 
      public static void main(String[] args) {     Holder <?> unbounded = new Holder <Long>(); 
        Long lng = 1L;     // OK, but can only return Object: 
        Object r11 = wildSubtype(unbounded, lng);     // wildSupertype(unbounded, lng); // Error: 
        //  wildSupertype(Holder <? super T>,T) cannot be 
        //  applied to (Holder <capture of ?>,Long) } ///:~