“黑马程序员”声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)

 ------- android培训java培训、期待与您交流! ----------

package test;

import java.util.*;
public class ten {
public static void main(String[] args) {
TreeSet ts=new TreeSet();
ts.add(new Student("张三",23,78.9));
ts.add(new Student("李四",45,90.99));
ts.add(new Student("王五",45,90));
ts.add(new Student("赵六",34,89));
ts.add(new Student("小明",14,89.7));
Iterator it=ts.iterator();
   while(it.hasNext())
   {
    Student stu=(Student)it.next();
    System.out.println(stu.getName()+"...."+stu.getAge()+"....."+stu.getscore());
//System.out.println(it.next());
   }
}


}
class Student implements Comparable
{
private String name;
private int age;
private double score;
public int compareTo(Object obj)
{
if(!(obj instanceof Student))
throw new RuntimeException("不是学生类型");
Student s=(Student)obj;
if(this.age>s.age)
return 1;
if(this.age==s.age)
return 0;
return -1;
}
Student(String name,int age,double score)
{
this.name=name;
this.age=age;
this.score=score;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getscore()
{
return score;
}
}

你可能感兴趣的:(集合,继承,多态)