Java – ein generisches Tag-Problem
phpcn_u1582
phpcn_u1582 2017-05-17 10:09:25
0
1
599

Ein Neuling stellt eine allgemeine Frage

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_u1582
phpcn_u1582

Antworte allen(1)
为情所困

Java中的泛型都是使用了类型擦除,你这里的<T> 只是一个类型变量。这个方法里面也只是用来代表@param <T> the class of the objects in the collection

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!