【问题标题】:Overriding and Implementing toString覆盖和实现 toString
【发布时间】:2015-03-18 16:57:08
【问题描述】:

我正在为一个应用程序编写代码,该应用程序可以跟踪学生在校园自助餐厅购买食物的情况。有两个类 - Student,它包含重载的构造函数和适当的 getter 和 setter 方法; MealCard,它包含一个类变量来跟踪已发行的餐卡数量、适当的 getter 和 setter 方法、purchaseItem() 方法、purchasePoints() 方法和覆盖的 toString() 方法。还有一个Tester 类。

如何从 Student 覆盖 MealCard 中的 toString 方法?它不接受姓名、年龄或地址——我知道它们是私人的。

而在tester类中,如何实现toString()来显示用户信息?

到目前为止我的代码是:

public class Student {

// Instance Variables
private String name;
private int age;
private String address;

// Default Constructor
public Student() {
    this("Not Given", 0, "Not Given");
}

// Parameterized constructor that takes in values
public Student(String name, int age, String address) {
    this.name = name;
    this.age = age;
    this.address = address;
}

// Getters and Setters
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

public String getAddress(){
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

// toString() to be overriden
@Override
public String toString() {
    return "Name: " + this.name + "\n" + "Age: " + this.age + "\n" + "Address: " + this.address;
}
}

`

public class MealCard {

private static int numberOfMealCards;
private final static int DEFAULT_BALANCE = 1000;
private int itemValue;
private int topUpValue;
public int newBalance;

// Getters and Setters
public int getItemValue() {
    return itemValue;
}
public void setItemValue(int itemValue) {
    this.itemValue = itemValue;
}

public int getTopUpValue() {
    return topUpValue;
}
public void setTopUpValue(int topUpValue) {
    this.topUpValue = topUpValue;
}

// purchaseItem method for when students buy food
public int purchaseItem() {
    newBalance = DEFAULT_BALANCE - itemValue;
    return newBalance;
}

// purchasePoints method for students topping up their meal card balance
public int purchasePoints() {
    newBalance = DEFAULT_BALANCE + topUpValue;
    return newBalance;
}

// Overriden toString method
@Override
public String toString() {
    return "Name: " + getName + "\n" + "Age: " + getAge + "\n" + "Address: " + getAddress +
            "\n" + "Meal Card Balance: " + newBalance + "\n" + "Number of Meal Cards: " + numberOfMealCards;
}

}

【问题讨论】:

  • 超类已经有一个toString,所以你不需要在子类中重新发明它。子类中的toString 应该提供子类唯一数据的字符串表示形式,如果您需要超类中的任何内容,请直接使用super.toString() 调用它的方法。您可以像在 Student 类中一样使用相同的 @Override 注释来覆盖它。
  • 如果您不使用super.toString(),您还需要在 MealCard toString() 方法中的所有方法调用上加上一对括号
  • @RyanJ 没有继承,这仍然适用吗?
  • @LauraBerry 抱歉,我急于做出判断。如果没有继承,那么你就没有覆盖(从技术上讲,你总是覆盖 Objects 实现)。但是,覆盖Object 的toString 应该以与学生相同的方式完成,但对于您的MealCard 班级。它应该提供该类的字符串表示。包含对MealCard 的引用的类将使用该对象上的toString 方法来获取该信息。

标签: java overriding implementation tostring


【解决方案1】:

我认为在答案中显示这一点可能比在广泛的评论中更容易。

在类中提供toString 的实现归结为提供该类的String 表示,仅此而已。您要做的是为不相关的类 (Student) 提供字符串信息,而 MealCard 对此一无所知(也不应该)。

你如何完成你想要的可能是让Student 更新以保存对MealCard 的引用(阅读:每个学生都有一个 MealCard),并在你的@987654328 @实现toString,使用你的餐卡引用从餐卡类中获取toString数据。

例如:

public class MealCard {

    //... skipped over for simplicity

    @Override
    public String toString() {
        return "Item Value = " + this.getItemValue() + ", TopUpValue = " + this.getTopUpValue() + " ... "; // and so on...
    } 
}

然后在Student 中,引用MealCard 并在toString 中使用它

public class Student {

    // other student data
    MealCard mealCard;  // set in constructor

    // ... other methods

    @Override
    public String toString() {
        return " ... other student data... MealCard = " + getMealCard().toString(); // use the mealcard reference to get string info

    }
}

有意义吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2017-12-28
    • 2020-03-09
    • 1970-01-01
    相关资源
    最近更新 更多