2013-06-23 82 views
-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; 

和(你剛纔看到的是演示的一部分,下面是類的東西的一部分)toString方法不會打印出我的對象,而不是它的打印內存ADRESS

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; 

} 

}

+3

toString shoud不接受任何參數 – Anton

+1

^^ s/should/does/ –

回答

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; 
} 
+0

問題是存款/取款/費用/總計未定義爲類中的私有字段,所以java無法爲我找到它 –

+0

You可能試圖在同一個班級中代表多個對象。它有一個初始餘額和一個最終餘額;這些可以是班級成員。但是一個賬戶並不只有一筆存款或提款 - 它受到了一系列的影響。 –

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); 
} 
/* .. */ 
+0

所以我在課堂上遇到的問題是,當我使用費用,總額,退出和無參數存款時,找不到符號也不能真正理解this.deposit函數thingy:O –

+0

@ user2514022可以發佈整個類的源代碼嗎? – zacheusz

+1

我現在發佈了它 –

0

其他答案缺失的信息很重要,因爲當你撥打System.out.println(b0) java編譯器會自動插入對.toString()的調用 - 它是語言定義explained here的一部分。

因此,如其他地方所說,要打印出一個物體,你需要覆蓋這個方法。

相關問題