BankAccount b0, b1, b2, b3;
b1=new BankAccount();
b2=new BankAccount();
b3=new BankAccount();
for (int i=0; i<3; i++)
{
if (i==0)
b0=b1;
else if (i==1)
b0=b2;
else
b0=b3;
和(你刚才看到的是演示的一部分,下面是课堂内容的一部分)
public String toString(double deposit, double withdraw, double fee, double total) {
String str=name+"'s bank account statement:" + "\nInitial balance: " + balance +
"\nDeposit amount: " + deposit + "\nWithdraw amount: " + withdraw + "\nNumber of transactions: "
+ transac + "\nTotal bank fees: " + fee + "Final monthly balance: " + total;
return str;
和(演示,我没有包含所有代码,只是因为这仍然是一个开放的任务。
System.out.println(b0);
我真的不知道为什么它不打印字符串的东西:( 所有课程(将在以后删除)
public class BankAccount {
private String name;
private double balance;
private int transac;
public BankAccount() {
balance=0;
}
public void setName(String accountName) {
name=accountName;
}
public void setBalance(double accountBalance) {
balance=accountBalance;
}
public void setTransac(int accountTransac) {
transac=accountTransac;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public int getTransac() {
return transac;
}
public double getDeposit(double deposit) {
return balance+deposit;
}
public double getWithdraw(double deposit, double withdraw) {
return balance+deposit-withdraw;
}
public double getFee(double fee, int accountTransac, double deposit, double withdraw) {
fee=10;
if (transac<20)
fee+=transac*0.5;
else if (20<=transac && accountTransac<40)
fee+=transac*0.25;
else if (40<=transac && transac<60)
fee+=transac*0.2;
else
fee+=transac*0.1;
if (balance+deposit-withdraw<400)
fee+=15;
return fee;
}
public double finalBalance(double fee, int accountTransac, double deposit, double withdraw) {
double total=balance+deposit-withdraw-fee;
return total;
}
public String toString(double deposit, double withdraw, double fee, double total) {
toString(this.deposit, this.withdraw, this.fee, this.total);
String str=name+"'s bank account statement:" + "\nInitial balance: " + balance +
"\nDeposit amount: " + deposit + "\nWithdraw amount: " + withdraw + "\nNumber of transactions: "
+ transac + "\nTotal bank fees: " + fee + "Final monthly balance: " + total;
return str;
}
}
答案 0 :(得分:3)
你应该覆盖public String toString()
方法(不带参数):
如果你已经有方法public String toString(double deposit, double withdraw, double fee, double total)
,那么使用它:
class BankAccount {
public String toString() {
toString(this.deposit, this.withdraw, this.fee, this.total);
}
/* .. */
答案 1 :(得分:1)
你已经重载了toString()方法,为兄弟提供了一组不同的参数。
您想要不带参数覆盖toString()方法。
例如,如果您的其他toString()方法的所有参数都是成员字段:
public String toString() {
String str=name+"'s bank account statement:" + "\nInitial balance: " + balance +
"\nDeposit amount: " + deposit + "\nWithdraw amount: " + withdraw + "\nNumber of transactions: "
+ transac + "\nTotal bank fees: " + fee + "Final monthly balance: " + total;
return str;
}
答案 2 :(得分:0)
其他答案遗漏的重要信息是,当您调用System.out.println(b0)
时,java编译器会自动插入对.toString()
的调用 - 它是语言定义的一部分{{3} }。
因此,正如其他地方所说,要打印出一个对象,你需要覆盖这个方法。