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

时间:2018-03-30 18:50:41

标签: object reference subclass superclass

我有一个超级班"播放器"和3个子课程" BaseballPlayer"," FootballPlayer"和" BasketballPlayer" ...为了简单起见,我只会分享"播放器&#34 ;和" 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()没有返回任何值...

1 个答案:

答案 0 :(得分:0)

这应该有效:

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

查看this,例如System.out.printf()的使用情况。