2013-06-23 42 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的一部分。

因此,如其他地方所说,要打印出一个物体,你需要覆盖这个方法。

相关问题