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.   

    帮帮忙吧
    我的理解:
    对于 wildSubtype(Holder<? super T> holder, T arg)
    T是Long型,那么Holder参数传入只要是  ? extends Long   的Holder都可以,所以 unbounded 无界的可以对于 wildSupertype(Holder <? super T> holder, T arg) 
    Holder参数传入只要是  ? super Long   的Holder都可以,所以 unbounded 无界应该也可以啊为什么会错误啊!!!
      

  2.   

    代码太乱,你不会把代码放到 javacode内吗?你这样我看都不愿看你的代码
      

  3.   


    public class Student
    {
    private String name;
    private String sex;public Student()
    {
    }
    /*然后封装字段*/
    }
    public class Test
    {
    public static void main(String [] args)
    {
    Vector<Student> vector=new Vector<Student>();
    Student student=new Student();
    student.setName="aa";
    student.setSex="男";
    vector.add(student);
    }
    }这就是泛型的一个简单应用。可以参照一下。还有很多集合都可以这样用,查查资料即可。
    泛型支持的jdk版本1.5