【问题标题】:toString method not printing out my object, instead it's printing the memory adress [duplicate]toString 方法没有打印出我的对象,而是打印内存地址 [重复]
【发布时间】:2013-06-23 20:13:23
【问题描述】:
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;

和(demo,我没有包含所有代码,只是因为这仍然是一个开放式作业。

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;

}

}

【问题讨论】:

  • toString 不应该接受任何参数

标签: java object tostring


【解决方案1】:

你应该重写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);
}
/* .. */

【讨论】:

  • 所以我在课堂部分遇到的问题是,当我使用不带参数的费用、总额、提款和存款时,它找不到符号,我也不太明白这个。存款功能的东西:O
  • @user2514022 你能发布整个课程的源代码吗?
【解决方案2】:

您已经重载了 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;
}

【讨论】:

  • 问题是存款/取款/费用/总额未定义为类中的私有字段,因此 java 无法为我找到它们
  • 您可能试图在同一类中表示多个对象。它有一个初始余额和一个最终余额;这些可以是班级成员。但是一个账户并没有确切的存款或取款——它受到了一系列的影响。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    相关资源
    最近更新 更多