获取从超类到子类对象的存储数据

Get Stored Data From Superclass to Subclass Object

我有一个超级 class "Player" 和 3 个子 classes "BaseballPlayer", "FootballPlayer", 和 "BasketballPlayer"...为了简单起见,我将只分享 "Player" 和 "BaseballPlayer".

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

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

}

棒球Player.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;
    }
}

}

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

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() returns 没有值...

这应该有效:

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

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