当前位置: 首页 > 知识库问答 >
问题:

toString方法没有打印出我的对象,而是打印内存地址[duplicate]

秦诚
2023-03-14
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;

}

}

共有1个答案

宣高朗
2023-03-14

应重写公共字符串toString()方法(不带参数):

如果您已经有方法public String toString(double dependent,double deturning,double fee,double total),则使用

class BankAccount {

 public String toString() {
  toString(this.deposit, this.withdraw, this.fee, this.total);
}
/* .. */
 类似资料:
  • 问题内容: 我是Java的新手。现在,我正在研究equals和==以及对equals和toString的重新定义。 我想同时使用已重新定义的toString方法和从Object类继承的默认方法。 我未能使用该超级修饰符来实现该方法。 这仅用于教育目的。如果您可以看一下我代码中的注释,那么我想得到的内容将更加清楚。 你能在这里帮我吗? 我的代码是: 问题答案: 严格来说,您不能在纯Java中打印对象

  • 使用 Photoshop,您可以打印任何兼容的 3D 模型,而无需担心 3D 打印机的限制。在准备打印时,Photoshop 会自动使 3D 模型防水。Photoshop 还会生成必要的支撑结构(支架和底座),以确保您的 3D 打印能够成功完成。 准备打印 3D 对象 选择“窗口”>“工作区”>“3D”以切换到 3D 工作区。 在 Photoshop 中打开 3D 模型。如果需要,请在打开 3D

  • 问题内容: 无论如何,是否可以使用新的Swift语言从Objective-C 模拟? 例如: 问题答案: 现在,它已成为标准库的一部分:。 迅捷3 对于Swift 3,请使用:

  • 问题内容: 该代码似乎在运行,除了我得到的不是指定的(由用户)大小的矩阵,而是我认为的是堆地址这是当用户输入2作为大小然后输入4个数字时返回的内容: 输入矩阵大小:2逐行输入2 x 2矩阵:2 3 4 5行排序矩阵是… [[D @ 3c954549BUILD SUCCESSFUL(总时间:8秒) 这是代码…。谢谢您。 问题答案: Java数组不会覆盖,因此您将从中获取默认实现。相反,您可以使用li

  • 问题内容: 通常,如果我们仅使用它,它将显示为。如何在JavaScript中打印对象的所有内容参数? 问题答案: 如果您使用的是Firefox,则只需进行简单的调试即可。

  • 我应该在我的代码中执行哪些更改,以便它可以打印整个家族都尝试过的字符串,我只得到null。这只是一个非常简单的代码soo plss帮助。 我只得到了对象的地址,这个问题可以被认为是这个问题的重复,但我无法理解这里提供的解决方案。 这是我再次尝试的 这将返回null。