gpt4 book ai didi

java - 返回 toString 方法,不返回我想要的(Java)

转载 作者:行者123 更新时间:2023-11-29 08:36:41 24 4
gpt4 key购买 nike

我有一个项目,我正在创建一个带有 SavingsAccount 子类的 BankAccount 父类(super class)。一切正常,但我无法返回我特别想要的字符串。

示例:(修剪)

public class BankAccount {
public static final double MONTHS_IN_YEAR = 12.0;

private String myCustomerName;
private double myAccountBalance;
private double myInterestRate;
protected int myMonthlyWithdrawCount;
protected double myMonthlyServiceCharges;

public BankAccount(final String theNameOfOwner,
final double theInterestRate) {
myCustomerName = theNameOfOwner;
myAccountBalance = 0.0;
myInterestRate = theInterestRate;
myMonthlyWithdrawCount = 0;
myMonthlyServiceCharges = 0.0;
}

public String toString() {
String result = "";
result += String.format("BankAccount[owner: %s, balance: %.2f,",
myCustomerName, myAccountBalance);
result += String.format(" interest rate: %.2f,", myInterestRate);
result += String.format("\n\t\t ");
result += String.format("number of withdrawals this month: %d,",
myMonthlyWithdrawCount);
result += String.format(" service charges for this month: %.2f]",
myMonthlyServiceCharges);
return result;
}
}

驱动类将使用 BankAccount 的 toString 方法并打印:

BankAccount[owner: John Doe, balance: 0.00, interest rate: 0.05,
number of withdrawals this month: 0, service charges for this month: 0.00]

(这对这个父类(super class)来说是完美的)

然而,这里出现了子类 SavingsAccount

public class SavingsAccount extends BankAccount {
public static final double SAVINGS_THRESHOLD = 25.0;

private boolean myStatusIsActive;

public SavingsAccount(final String theNameOfOwner,
final double theInterestRate) {
super(theNameOfOwner, theInterestRate);
myStatusIsActive = false;
if (super.getBalance() >= SAVINGS_THRESHOLD) {
myStatusIsActive = true;
}
}

public String toString() {
String result = "";
result += "SavingsAccount";
result += super.toString();
return result;
}
}

调用SavingsAccount的toString方法时,打印:

SavingsAccountBankAccount[所有者:Dan Doe,余额:0.00,利率:0.05, 本月取款笔数:0,本月手续费:0.00】

(我不希望包含 BankAccount,我只希望它打印“SavingsAccount” header ,然后直接转到 [owner : Dan Doe,"

我试过让它首先返回“SavingsAccount”,它正确地返回了,但是当调用 super.toString() 时,它最终也返回了 BankAccount header 我不想要。

关于如何解决这个问题有什么想法吗?

最佳答案

我能想到的几个选项:

  1. 将“BankAccount”替换为“SavingsAccount”,而不仅仅是附加它:

    @Override
    public String toString() {
    return super.toString().replaceFirst("BankAccount", "SavingsAccount");
    }
  2. 将详细信息部分移到它自己的方法中,并使用适当的 header 从每个 toString() 调用它:

    // BankAccount.java
    @Override
    public String toString() {
    return "BankAccount" + getDetailsString();
    }

    protected String getDetailsString() {
    String result = String.format("[owner: %s, balance: %.2f,",
    myCustomerName, myAccountBalance);
    // ...
    return result;
    }

    // SavingsAccount.java
    @Override
    public String toString() {
    return "SavingsAccount" + getDetailsString();
    }
  3. 获取父类(super class)中的运行时类名,这样就不需要完全覆盖它:

    @Override
    public String toString() {
    String result = String.format("%s[owner: %s, balance: %.2f,",
    getClass().getSimpleName(), myCustomerName, myAccountBalance);
    // ...
    return result;
    }

关于java - 返回 toString 方法,不返回我想要的(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43578136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com