老师留下的作业是从控制台接受学生参数,格式如下
zhangsan,50,50,50
接受多名学生参数.并且按总分数排序,最高分排在前.写入一个文件夹中~,
详细代码如下,~用到呢omparable接口~ 这个接口我还不是太懂;
public class Student implements Comparable<Student> {
private String name;
int ma,cn,en;
int sum;
public Student(String name, int ma, int cn, int en ) {
this.name = name;
this.ma = ma;
this.cn = cn;
this.en = en;
this.sum = ma+cn+en;
}
public String getName(){
return name;
}
public int getSum (){
return sum;
}
public int hashCode(){
return name.hashCode()+sum*78;
}
public boolean equals (Object obj){
if(!(obj instanceof Student)){
throw new ClassCastException("类型不匹配");
}
Student s = (Student)obj;
return this.name.equals(s.name)&&this.sum==s.sum;
}
@Override
public int compareTo(Student o) {
int num = new Integer(this.sum).compareTo(new Integer(o.sum));
if(num==0)
return this.name.compareTo(o.name);
return num;
}
public String toString (){
return "student["+ name+","+ma+","+cn+","+en+"]";
}
}
这个类实现这个接口有什么用啊?
而且他怎么自己可以比较就按分高的 排序呢 谁给我讲下个接口呢~ Comparable
|