0

我对覆盖超类中的方法有点困惑。谁能帮我解决这个加粗的问题?我不确定如何在“篮球”的主要方法中调用方法“说”。

一个篮球队有12名球员。每个球员都是 3 种类型中的一种 - 中锋、后卫、前锋。

一支球队有4名中锋、4名前锋和4名后卫。

每个玩家都有一个唯一的名字和一个唯一的号码。

定义一个基类 Player。Player 类有一个名称成员、一个字符串和一个数字成员、一个整数。

从 Player 类派生一个 Center 类、一个 Guard 类和 Forward 类。

为基类和派生类定义适当的构造函数、访问器和修改器。

定义一个基类方法,Say。Says 显示玩家的姓名和号码

覆盖派生类中的基类 Say 方法。派生类的 Says 显示玩家的位置并调用基类 Say。基类 Say 和派生类 Says 一起显示玩家的姓名、号码和类型

public class Player
{
    protected String firstName;
    protected String lastName;
    protected int num;

    public Player(String first, String last, int jerseyNum)
    {
        firstName = first;
        lastName = last;
        num = jerseyNum;
    }

    public void setFirstName(String first)
    {
        firstName = first;

    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setLastName(String last)
    {
        lastName = last;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setNum(int jerseyNum)
    {
        num = jerseyNum;
    }

    public int getNum()
    {
        return num;
    }

    public void say()
    {
        System.out.printf("The player's name is %s %s and his jersey number is %d.\n", firstName, lastName, num);

    }
}

public class Center extends Player
{
    private String center;

    public Center(String first, String last, int num, String position)
    {
        super(first, last, num);
        center = position;
    }

    public void setPosition(String position)
    {
        center = position;
    }

    public String getPosition()
    {
        return center;
    }

    @Override
    public void say()
    {
        System.out.printf("This player plays the %s position", center);
    }
}


public class Forward extends Player
{
    private String forward;

    public Forward(String first, String last, int num, String position)
    {
        super(first, last, num);
        forward = position;
    }

    public void setPosition(String position)
    {
        forward = position;
    }

    public String getPosition()
    {
        return forward;
    }

    @Override
    public void say()
    {
        System.out.printf("This player plays the %s position", forward);
    }
}   



public class Guard extends Player
{
    private String guard;

    public Guard(String first, String last, int num, String position)
    {
        super(first, last, num);
        guard = position;
    }

    public void setPosition(String position)
    {
        guard = position;
    }

    public String getPosition()
    {
        return guard;
    }

    @Override
    public void say()
    {
        System.out.printf("This player plays the %s position", guard);
    }
}
public class basketball
{
    public static void main(String[] Args)
    {
        //Player bballPlayer =  new Player("Jon", "Jones", 25);
        Center positionCenter = new Center("Jon", "Jones", 25, "Center");

        positionCenter.say();
    }
}
4

2 回答 2

4

super.您可以使用如下所示的示例调用超类方法:

@Override
public void say() {
    super.say();
    System.out.printf(" This player plays the %s position", guard);
}
于 2013-05-01T03:08:31.770 回答
3

您需要通过执行以下操作来调用 Player 类中的 super 方法:

@Override
public void say()
{
    super.say();
    System.out.printf("This player plays the %s position", center);
}
于 2013-05-01T03:08:50.673 回答