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

返回toString方法,而不是返回我想要的(Java)

高云瀚
2023-03-14

我有一个项目,其中我创建了一个BankAccount超级类和一个SavingsAccount子类。一切都很好,但我在返回我特别想要的字符串时遇到了麻烦。

示例:(裁剪)

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] 

(这对于这个超类来说是完美的)

但是,下面是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”标头,然后直接转到“[owner:Dan Doe,”

我试着让它首先返回“savingsaccount”,这是正确的,但是当调用super.tostring()时,它也返回了我不想要的bankaccount头。

我对如何解决这个问题有什么想法吗?

共有1个答案

殳飞扬
2023-03-14

我能想到几个选择:

>

  • 将“bankaccount”替换为“savingsaccount”,而不只是追加:

    @Override
    public String toString() {
        return super.toString().replaceFirst("BankAccount", "SavingsAccount");
    }
    

    将details部分移动到它自己的方法中,并从每个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();
    }
    
    @Override
    public String toString() {
        String result = String.format("%s[owner: %s, balance: %.2f,",
                getClass().getSimpleName(), myCustomerName, myAccountBalance);
        // ...
        return result;
    }
    

  •  类似资料:
    • Android Kotlin,<代码>api。openOrder返回类型为BaseResponse 我想要的代码是,如果配置文件。值不为null,API返回BaseResponse 但android studio提示,这种有趣的返回类型是任何,如何修复? 排除if-else用法,因为配置文件。值具有许多属性,并且具有配置文件。价值用户名,

    • 我试图让2支球队互相比赛。当我说团队1.玩(团队2)时,我称之为;当 i 生成的数字小于 0.5 时,team2 应获胜,如果大于 0.5,则团队 1 应获胜。当团队 1 获胜时,它会正确显示为尼克斯,但当团队 2 获胜时,它会显示内存地址。我怎么能让它正确地说网是赢的,而不是team@78987neu73

    • 我得到了这个错误:这个表达式的类型是'void',所以它的值不能被使用。尝试检查是否使用了正确的API;可能会有一个函数或调用返回您意想不到的void。还要检查类型参数和变量,它们也可能是空的。 代码: null null 我不明白这是什么。我是新手。这是我的第一个应用程序。有人能帮我一下吗。

    • 问题内容: 我有一类这样的方法: 我如何在另一个类中调用此方法? 问题答案: 1. 如果要从中调用该方法的类位于同一包中,则创建该类的实例并调用该方法。 2. 使用 3. 最好有个赞等等。 例如:

    • 编辑问题以包括所需的行为、特定问题或错误,以及重现问题所需的最短代码。这将有助于其他人回答这个问题。 代码不返回值,而是返回“?”。编译时我没有遇到任何错误。请协助。 代码需要返回需要支付的剩余金额。输出代码1代码2代码3代码4