gpt4 book ai didi

java - 重写并实现 toString

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:27 25 4
gpt4 key购买 nike

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

如何从 Student 重写 MealCard 中的 toString 方法?它不会接受姓名、年龄或地址——而且我知道它们是私有(private)的。

在测试器类中,如何实现 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 的实现归结为提供该类的 String 表示形式,仅此而已。您想要做的是为不相关的类 (Student) 提供字符串信息,其中 MealCard 一无所知(也不应该)。

如何实现您想要的效果可能是让 Student 更新以保存对 MealCard 的引用(阅读:每个学生都有一个 MealCard),并在 toStringStudent 实现中,使用您的餐卡引用从餐卡类获取 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

}
}

有道理吗?

关于java - 重写并实现 toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29128125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com