-5
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;

}

}

4

3 回答 3

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);
}
/* .. */
于 2013-06-23T20:16:12.783 回答
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;
}
于 2013-06-23T20:14:21.097 回答
0

其他答案缺少的重要信息是,当您调用System.out.println(b0)java 编译器时,它会自动插入调用- 这是此处解释.toString()的语言定义的一部分。

因此,正如其他地方所说,要打印出您需要覆盖此方法的对象。

于 2013-06-23T23:07:10.623 回答