/*
 * 比较器的使用,实现 Comparable接口
 * 功能:实现对象的多条件比较
 */
package com.array;
import java.util.Arrays;
public class compareto {
    public static void main(String[] args){
        Student arrStu[] = {
                new Student("王明",10,55),
                new Student("孙丽", 50, 55),
                new Student("李剑",20, 80)};
        Arrays.sort(arrStu);
        for(int i=0;i<arrStu.length;i++){
            System.out.println(arrStu[i]);
        }
    }
}
class Student implements Comparable<Student>{
    private String name="";
    private int age =0;
    private float score;
    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 float getScore() {
        return score;
    }
    public void setScore(float score) {
        this.score = score;
    }
    public Student(String name,int age,float score){
        this.setName(name);
        this.setAge(age);
        this.setScore(score);
    }
    public String toString(){
        return this.getName()+"    分数:"+this.getScore()+"     年龄:"+this.getAge();
    }
    public int compareTo(Student stu){
        //1>先比较分数
        if(this.getScore()>stu.getScore()){
            return 1;
        }else if(this.getScore()<stu.getScore()){
                return -1;
        }else {
            //2>再比较年龄
            if(this.getAge()>stu.getAge()){
                return 1;
            }else if(this.getAge()<stu.getAge()){
                return -1;
            }else{
                return 0;
            }
        }
    }
}

wKiom1LhuhvwAQ9QAAB-eOT5ayU629.jpg