Java:实现子类与父类的继承以及各自类的方法,且运用Comparator进行多排序输出(第十周)

版权声明:根据《中华人民共和国著作权法》,如需转载请标明来源并联系作者进行授权。本文作者保留依法追究未经授权转载等侵犯作者著作权等的违法行为之权利。 https://blog.csdn.net/qq_41933331/article/details/81977988

题目来源:大工慕课 链接
作者:Caleb Sung

题目要求

注:本题基于上周的题目进行方法升级操作,并删除了一个子类。
查看上周题目请移步文章:Java:实现多子类与父类的继承,并实现各自类的方法(第九周)

实现此UML类图中所示的三个类:
这里写图片描述
1. 运动员的getDescription()方法应该返回一个字符串,例如“An Athlete XXXName with height of XXX and weight of XXX”。
2. BasketballPlayer中的getDescription()应该返回一个字符串,例如“A basketball player XXXName with shooting level of XXX”。
3. BaseballPlayer中的getDescription()应该返回一个字符串,例如“A baseball player XXXName with hitting level of XXX”。
4. 运动员之间的默认比较基于他们的名字。
5. BasketballPlayer的默认比较基于他们的shootLv
6. 在主函数中,实现一个由3位运动员组成的数组,然后按以下顺序对数组进行排序:名字升序、名字降序、身高降序。
7. 在主功能中,实现一个由3名篮球员组成的数组,然后按以下顺序对数组进行排序:shootLv升序、shootLv降序。


参考代码

Athlete类(父类)

class Athlete implements Comparable<Athlete>{
    private String name;
    private int height;
    private int weight;

    public Athlete(String n, int h, int w) {
        name = n;
        height = h;
        weight = w;
    }

    public String getName() {
        return name;
    }


    public int getHeight() {
        return height;
    }


    public int getWeight() {
        return weight;
    }

    public String toString() {
        return String.format("An Athlete %s with height of %d and weight of %d", name, weight, height);
    }


    public int compareTo(Athlete arg0) {
        return this.name.compareTo(arg0.name);
    }
}

用于按名字/身高比较运动员的三个类

class CompAthleteNameAc implements Comparator<Athlete>{
    public int compare(Athlete arg0, Athlete arg1) {
        return arg0.getName().compareTo(arg1.getName());
    }
}

class CompAthleteNameDec implements Comparator<Athlete>{
    public int compare(Athlete arg0, Athlete arg1) {
        return arg1.getName().compareTo(arg0.getName());
    }
}

class CompAthleteHeightDec implements Comparator<Athlete>{
    public int compare(Athlete arg0, Athlete arg1) {
        return arg1.getHeight() - arg0.getHeight();
    }
}

BasketballPlayer类(子类)

class BasketballPlayer extends Athlete{
    private int shootLv;

    public BasketballPlayer(String n, int h, int w, int sl) {
        super(n, h, w);
        shootLv = sl;
    }

    public String toString() {
        return String.format("A basketball player %s with shooting level of %d", getName(), shootLv);
    }

    public int getShootLv() {
        return shootLv;
    }

    public int compareTo(Athlete arg0) {
        BasketballPlayer p = (BasketballPlayer) arg0;
        return this.shootLv - p.shootLv;
    }
}

用于按ShootLv比较运动员的两个类

class CompBasketballPlayerShootLvAc implements Comparator<BasketballPlayer>{
    public int compare(BasketballPlayer arg0, BasketballPlayer arg1) {
        return arg0.getShootLv() - arg1.getShootLv();
    }
}

class CompBasketballPlayerShootLvDec implements Comparator<BasketballPlayer>{
    public int compare(BasketballPlayer arg0, BasketballPlayer arg1) {
        return arg1.getShootLv() - arg0.getShootLv();
    }
}

imports、主函数与测试用例

import java.util.Arrays;
import java.util.Comparator;

public class ArraySortTest {

    public static void main(String[] args) {
        Athlete[] athletes = new Athlete[3];
        BasketballPlayer[] basketballplayers = new BasketballPlayer[3];

        //Tests
        athletes[0] = new Athlete("HanMei", 180, 60);
        athletes[1] = new Athlete("Lilei", 190, 70);
        athletes[2] = new Athlete("Tom", 170, 50);
        basketballplayers[0] = new BasketballPlayer("Jerry", 175, 55, 4);
        basketballplayers[1] = new BasketballPlayer("Sam", 195, 75, 6);
        basketballplayers[2] = new BasketballPlayer("Amy", 165, 65, 5);

        //System.out.println(Arrays.toString(athletes));
        System.out.println("The default comparison between Athletes based on their names:");
        Arrays.sort(athletes);
        for(Athlete a : athletes)
            System.out.println(a);

        System.out.println("\nThe default comparison between BasketballPlayer based on their shootLv(s):");
        Arrays.sort(basketballplayers);
        for(BasketballPlayer b : basketballplayers)
            System.out.println(b);

        System.out.println("\nSort the Athlete array in the order of Name ascending:");
        Arrays.sort(athletes, new CompAthleteNameAc());
        for(Athlete a : athletes)
            System.out.println(a);

        System.out.println("\nSort the Athlete array in the order of Name descending:");
        Arrays.sort(athletes, new CompAthleteNameDec());
        for(Athlete a : athletes)
            System.out.println(a);

        System.out.println("\nSort the Athlete array in the order of Height descending:");
        Arrays.sort(athletes, new CompAthleteHeightDec());
        for(Athlete a : athletes)
            System.out.println(a);

        System.out.println("\nSort the BasketballPlayer array in the order of ShootLv ascending:");
        Arrays.sort(basketballplayers, new CompBasketballPlayerShootLvAc());
        for(BasketballPlayer b : basketballplayers)
            System.out.println(b);

        System.out.println("\nSort the BasketballPlayer array in the order of ShootLv descending:");
        Arrays.sort(basketballplayers, new CompBasketballPlayerShootLvDec());
        for(BasketballPlayer b : basketballplayers)
            System.out.println(b);
    }
}

运行结果

在IDEA里会提示Athlete类的getWeight()未被使用,注释掉也无伤大雅。

The default comparison between Athletes based on their names:
An Athlete HanMei with height of 60 and weight of 180
An Athlete Lilei with height of 70 and weight of 190
An Athlete Tom with height of 50 and weight of 170

The default comparison between BasketballPlayer based on their shootLv(s):
A basketball player Jerry with shooting level of 4
A basketball player Amy with shooting level of 5
A basketball player Sam with shooting level of 6

Sort the Athlete array in the order of Name ascending:
An Athlete HanMei with height of 60 and weight of 180
An Athlete Lilei with height of 70 and weight of 190
An Athlete Tom with height of 50 and weight of 170

Sort the Athlete array in the order of Name descending:
An Athlete Tom with height of 50 and weight of 170
An Athlete Lilei with height of 70 and weight of 190
An Athlete HanMei with height of 60 and weight of 180

Sort the Athlete array in the order of Height descending:
An Athlete Lilei with height of 70 and weight of 190
An Athlete HanMei with height of 60 and weight of 180
An Athlete Tom with height of 50 and weight of 170

Sort the BasketballPlayer array in the order of ShootLv ascending:
A basketball player Jerry with shooting level of 4
A basketball player Amy with shooting level of 5
A basketball player Sam with shooting level of 6

Sort the BasketballPlayer array in the order of ShootLv descending:
A basketball player Sam with shooting level of 6
A basketball player Amy with shooting level of 5
A basketball player Jerry with shooting level of 4

猜你喜欢

转载自blog.csdn.net/qq_41933331/article/details/81977988