我正在使用“数据结构”一书中的练习,似乎我正在打印参考地址而不是实际内容?有人可以看看我的代码并帮助我吗?谢谢你的时间。
public class CreditCard {
// instance variables
private String customer;
private String bank;
private String account;
private int limit;
protected double balance;
// constructors - account for all cases, one with a bal, and one without
public CreditCard(String cust, String bk, String acnt, int lim, double bal){
customer = cust;
bank = bk;
account = acnt;
limit = lim;
balance = bal;
}
public CreditCard(String cust, String bk, String acnt, int lim){
this(cust, bk, acnt, lim, - 0.0);
}
// accessors
public String getCustomer(){return customer;}
public String getBank(){return bank;}
public String getAccount(){return account;}
public double getLimit(){return limit;}
public double getBalance(){return balance;}
// updaters
public boolean charge(double price){
if(price + balance > limit){
return false;
}
else balance += price;
return true;
}
public void makePayment(double amount){
balance -= amount;
}
// utility (static)
public static void printSummary(CreditCard card){
System.out.println("Customer = " + card.customer);
System.out.println("Bank = " + card.bank);
System.out.println("Account = " + card.account);
System.out.println("Limit = " + card.limit);
System.out.println("Balance = " + card.balance);
}
} //结束类CreditCard
答案 0 :(得分:1)
添加toString
方法(只需覆盖一个)。
public class CreditCard {
// instance variables
private String customer;
private String bank;
private String account;
private int limit;
protected double balance;
// constructors - account for all cases, one with a bal, and one without
public CreditCard(String cust, String bk, String acnt, int lim, double bal) {
customer = cust;
bank = bk;
account = acnt;
limit = lim;
balance = bal;
}
public CreditCard(String cust, String bk, String acnt, int lim) {
this(cust, bk, acnt, lim, -0.0);
}
// accessors
public String getCustomer() {
return customer;
}
public String getBank() {
return bank;
}
public String getAccount() {
return account;
}
public double getLimit() {
return limit;
}
public double getBalance() {
return balance;
}
// updaters
public boolean charge(double price) {
if (price + balance > limit) {
return false;
} else {
balance += price;
}
return true;
}
public void makePayment(double amount) {
balance -= amount;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("CreditCard{");
sb.append("customer='").append(customer).append('\'');
sb.append(", bank='").append(bank).append('\'');
sb.append(", account='").append(account).append('\'');
sb.append(", limit=").append(limit);
sb.append(", balance=").append(balance);
sb.append('}');
return sb.toString();
}
// you do not need this util method because of it converts to
public static void printSummary(CreditCard card) {
System.out.println("CreditCard = " + card);
}
}// end class CreditCard
答案 1 :(得分:0)
你得到的是toString()
类CreditCard
的实现。
引用其javadoc:
结果应该是一个简洁但信息丰富的表示,便于人们阅读。建议所有子类都覆盖此方法。
这意味着,您需要在类@Override
public String toString() {
return getClass().getName()
+ "[ customer = " + customer
+ ", bank = " + bank
+ ", account = " + account
+ ", limit = " + limit
+ ", balance = " + balance
+ "]";
}
中使用自己的\n
实现覆盖它。
它可以这么简单:
public static void printSummary(CreditCard card)
如果您更喜欢多行输出,可以在上面的部分字符串中插入一些CreditCard.printSummary(creditCard);
。
顺便说一下:
你不再需要你的方法System.out.println(creditCard);
了。
而不是使用
toString()
你现在可以简单地使用
{{1}}
将调用您的{{1}}方法。