本帖最后由 倪杭乘 于 2012-5-9 13:26 编辑
程序比较长,直接看后面的就行,实现comparable接口,覆盖compareTo方法中- public class Student implements Comparable<Student>{
- private String name;
- private int ch,en,ma,sum;
- public Student(String name, int ch, int en, int ma) {
- super();
- this.name = name;
- this.ch = ch;
- this.en = en;
- this.ma = ma;
- sum = ch+en+ma;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getCh() {
- return ch;
- }
- public void setCh(int ch) {
- this.ch = ch;
- }
- public int getEn() {
- return en;
- }
- public void setEn(int en) {
- this.en = en;
- }
- public int getMa() {
- return ma;
- }
- public void setMa(int ma) {
- this.ma = ma;
- }
- public int getSum() {
- return sum;
- }
- public void setSum(int sum) {
- this.sum = sum;
- }
- @Override
- public int compareTo(Student o) {
-
- int temp = sum-o.sum;
- return temp==0?o.name.compareTo(name):temp;
- }
-
- }
复制代码 |