studentagesort.java

来自「关于java面向对象系统分析方面的课件」· Java 代码 · 共 54 行

JAVA
54
字号
/*
 * StudentAgeSort
 * 2007
 * 按学生的年龄排序
 */
public class StudentAgeSort extends GenericSort {

    public int compare(Object o1, Object o2) {
        int o1Age = ((Student) o1).getAge();
        int o2Age = ((Student) o2).getAge();
        int result = 0;
        if (o1Age > o2Age) {
            result = 1;
        } else if (o1Age < o2Age) {
            result = -1;
        }
        return result;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.setName("王二");
        stu1.setAge(17);

        Student stu2 = new Student();
        stu2.setName("张三");
        stu2.setAge(15);

        Student stu3 = new Student();
        stu3.setName("李四");
        stu3.setAge(19);

        Student stu4 = new Student();
        stu4.setName("赵六");
        stu4.setAge(18);

        Student[] stus = { stu1, stu2, stu3, stu4 };
        Sort sort = new StudentAgeSort();
        sort.upSort(stus);
        for (int i = 0; i < stus.length; i++) {
            System.out.println(((Student) stus[i]).getName());
        }

        sort.downSort(stus);
        for (int i = 0; i < stus.length; i++) {
            System.out.println(((Student) stus[i]).getName());
        }
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?