覆盖并实现toString

时间:2015-03-18 16:57:08

标签: java override implementation tostring

我正在为一个应用程序编写代码,该应用程序可以跟踪学生在校园自助餐厅购买食物的情况。有两个类 - 学生,它包含重载的构造函数和&适当的吸气剂二传手法;和 MealCard ,它包含一个类变量来跟踪发出的餐卡数量,适当的吸气剂和放大器数量。 setter方法,purchaseItem()方法,purchasePoints()方法&重写的toString()方法。还有一个 Tester类

如何从学生覆盖 MealCard 中的toString方法?它不接受姓名,年龄或地址 - 我知道他们是私人的。

在测试者类中,如何实现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;
}

}

1 个答案:

答案 0 :(得分:1)

我认为在答案中展示这一点可能更容易,而不是在广泛的评论中。

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

如何完成所需内容可能会Student更新以保留对MealCard的引用(阅读:每个学生都有 MealCard) ,在Student 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

    }
}

有意义吗?