请问我下面的程序哪里错了啊 困扰了好久了
import java.util.Iterator;import java.util.ArrayList;
import java.util.Collection;
public class 练习获取集合最大值
{
public static void main(String[] args)
{
ArrayList<? extends Person> al = new ArrayList<>();
al.add(new Student("ahjklhkalhkjlhf",53));
al.add(new Student("xcvbxcvsdf",67));
al.add(new Person("asdfasdf",45));
al.add(new Person("ruytry",20));
Person max=getMax(al);
System.out.println(max);
}
public static <T extends Comparable<? super T>> T getMax(Collection <? extends T> c)
{
//迭代此从哪个集合那拿的元素,里面的类型就要和其一致
//所以下面是? extends T不是? super T
Iterator<? extends T> it = c.iterator();
//思路1.定义变量记录集合中其中一个
T max =it.next();
//2.遍历集合中的元素
while(it.hasNext())
{
if(((it.next())>0))
max=it.next();
}
return max;
}
}
class Person
{
private String name;
private int age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age=age;
}
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
public int compareTo(Person p)
{
int temp = this.age-p.age;
return temp==0?this.(p.name):temp;
}
}
class Student extends Person
{
public Student(String name,int age)
{
super(name,age);
}
}