如何在子类中使用相同的方法名调用超类的方法

时间:2013-05-01 03:03:22

标签: java

我对覆盖超类中的方法感到困惑。任何人都可以帮助我解决这个粗体问题吗?我不太确定如何在“篮球”的主要方法中调用方法'说'。

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

一支球队有4个中锋,4个前锋和4个后卫。

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

定义基类播放器。 Player类具有名称成员,字符串和数字成员,即整数。

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

为基类和派生类定义适当的构造函数,访问器和mutator。

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

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

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();
    }
}

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:3)

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

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