首頁  >  問答  >  主體

java - 一個泛型標籤問題

新手問一個泛型問題

public static void main(String[] args) {
        ArrayList<Student> al = new ArrayList<>();
        
        al.add(new Student("大石榴",17,100));
        al.add(new Student("地雷",20,80));
        al.add(new Student("张大炮",21,60));
        
        Comparator<Student> cp = new Comparator<Student>() {
            
            @Override
            public int compare(Student o1, Student o2) {
                
                return o1.getAge() - o2.getAge();
            }
        };
    
        Collections.max(al, cp);
        
        //public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
        //这是max方法的源码.
        //    <T>    这个泛型在哪获取到的?
        
        for(Student st : al){
            System.out.println(st);
        }
    }
phpcn_u1582phpcn_u15822712 天前598

全部回覆(1)我來回復

  • 为情所困

    为情所困2017-05-17 10:11:25

    Java中的泛型都是使用了類型擦除,你這裡的 只是一個類型變數。這個方法裡面也只是用來代表@param <T> the class of the objects in the collection

    回覆
    0
  • 取消回覆