【问题标题】:Get Stored Data From Superclass to Subclass Object从超类到子类对象获取存储的数据
【发布时间】:2018-09-09 19:30:39
【问题描述】:

我有一个超类“Player”和 3 个子类“BaseballPlayer”、“FootballPlayer”和“BasketballPlayer”...为简单起见,我只分享“Player”和“BaseballPlayer”。

我正在尝试将数据存储到这些类的对象中,但我似乎无法获取并打印出来。

Player.java

public class Player {

private int id;
private String playerName;
private String teamName;
private String position;
private double salary;
private double commisionRate;

/* Default Constructor */
public Player() {
}

/* Constructor */
public Player(int id, String playerName, String teamName, String position, double salary, double commisionRate) {
    this.id = id;
    this.playerName = playerName;
    this.teamName = teamName;
    this.position = position;
    this.salary = salary;
    this.commisionRate = commisionRate;
}

/* Getters */ 
public int getID() {
    return id;
}

public String getPlayerName() {
    return playerName;
}

public String getTeamName() {
    return teamName;
}

public String getPosition() {
    return position;
}

public double getSalary() {
    return salary;
}

public double getCommisionRate() {
    return commisionRate;
}

/* Setters */
public void setID(int id) {
    this.id = id;
}

public void setPlayerName(String playerName) {
    this.playerName = playerName;
}

public void setTeamName(String teamName) {
    this.teamName = teamName;
}

public void setPosition(String position) {
    this.position = position;
}

public void setSalary(double salary) {
    this.salary = salary;
}

public void setCommision(double commisionRate) {
    this.commisionRate = commisionRate;
}

/* Effectors */
public double calcCommision()
{
    return salary * commisionRate;
}

}

BaseballPlayer.java

public class BaseballPlayer extends Player {

public static final double Threshold = 0.25;

private int numOfHits;
private int numAtBat;

/* Default Constructor */
public BaseballPlayer() {
}

/* Constructor */
public BaseballPlayer(int id, String playerName, String teamName, String position, double salary, double commisionRate, int numOfHits, int numAtBat) {
    super(id, playerName, teamName, position, salary, commisionRate);
    this.numOfHits = numOfHits;
    this.numAtBat = numAtBat;
}

/* Getters */
public int getNumOfHits() {
    return numOfHits;
}

public int getNumAtBat() {
    return numAtBat;
}

/* Setters */
public void setNumOfHits(int numOfHits) {
    this.numOfHits = numOfHits;
}

public void setNumAtBat(int numAtBat) {
    this.numAtBat = numAtBat;
}

/* Effectors */
public double calcStats()
{
    return numOfHits / numAtBat;
}

public boolean detStatus()
{
    if(calcStats() > Threshold) {
        return true;
    } else {
        return false;
    }
}

}

假设我用这两个类创建了一个对象...

Player BaseballPlayer1 = new BaseballPlayer(4, "Jeff Jefferson", "Kentucky Kangaroos", "3rd Base", 340000.00, 0.02, 40, 5);

但是,每当我调用此对象获取数据时,它什么也不打印...

System.out.printf("Player ID: ", BaseballPlayer1.getID());

我错过了什么?我查看了 SO 和 Google,我的 IDE 没有显示任何错误,但 getID() 没有返回任何值...

【问题讨论】:

    标签: object reference subclass superclass


    【解决方案1】:

    这应该可行:

    System.out.printf("Player ID: %d", baseballPlayer.getID());
    

    看看this,例如 System.out.printf() 的用法。

    【讨论】:

    • 我不敢相信我错过了这么简单的事情。哇。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多