【问题标题】:How to prevent an object from being written-over inside a boolean method?如何防止对象在布尔方法中被覆盖?
【发布时间】:2018-09-11 08:54:49
【问题描述】:

我正在尝试执行以下操作:

编写一个将帐户添加到模式的布尔方法。

一个。它应该有一个汇率参数和一个帐户类型参数。

b.如果他们还没有一个帐户,则此帐户将成为读者的第一个帐户;如果他们已经有一个帐户,则该帐户将成为他们的第二个帐户。

c。如果他们已经有两个帐户,则该方法返回 false,否则返回 真的。

这是代码:

public class Assignment6 {
    public static void main(String[] args) {
        System.out.println(BankPatron.addAccount(11,AccountType.CD));
        System.out.println(BankPatron.addAccount(12,AccountType.CD));
        System.out.println(BankPatron.addAccount(13,AccountType.CD));
    }
}

class BankPatron {
    public static BankAccount account1;
    public static BankAccount account2;
    public static Boolean addAccount(double rate, AccountType type) {
        if (account1 == null) {
            BankAccount account1 = new BankAccount("","",rate,type);
            System.out.println(account1.getRate());
            return true;
        }
        else if (account2 == null) {
            BankAccount account2 = new BankAccount("","",rate,type);
            System.out.println(account2.getRate());
            return true;
        }
        else {
            return false;
        }
    }
}

这会返回:

11.0
true
12.0
true
13.0
true

这意味着 account1 对象被改写了 3 次,对吗?如何在 addAccount 完成后保存 account1,这样一旦 addAccount 再次运行,它就会看到 account1 不再为空?

【问题讨论】:

    标签: java object methods null boolean


    【解决方案1】:

    if-block 中的局部变量会影响您的静态字段。

    代替:

    if (account1 == null) {
        BankAccount account1 = new BankAccount("", "", rate, type);
        // ...
    }
    

    应该是:

    if (account1 == null) {
        account1 = new BankAccount("", "", rate, type);
        // ...
    }
    

    很可能,您还需要重构 BankPatron 以不使用 static 字段/方法,而是使用实例字段/方法。

    这将允许你写:

    BankPatron bankPatron = new BankPatron();
    bankPatron.addAccount(11, AccountType.CD);
    // ...
    

    【讨论】:

      【解决方案2】:

      避免在 if 条件中重新声明 account1 和 account2。

      if (account1 == null) {
                  BankAccount account1 = new BankAccount("","",rate,type);
      ....
      else if (account2 == null) {
              BankAccount account2 = new BankAccount("","",rate,type);
      

      这应该是:

      if (account1 == null) {
                      account1 = new BankAccount("","",rate,type);
      .....
      else if (account2 == null) {
              account2 = new BankAccount("","",rate,type);
      

      如果你的程序是多线程的,那么让这个方法同步:

      public synchronized static Boolean addAccount(double rate, AccountType type) { 
      

      【讨论】:

        【解决方案3】:

        检查 account1 是否为空后,您将创建新的 BankAccount 对象并将其分配给局部变量 account1 而不是“account1”字段。 查看代码中的这一行

         BankAccount account1 = new BankAccount("","",rate,type);
        

        此 account1 变量的范围将在 if 块内。因此,您的分配将发生在本地“account1”对象而不是静态字段“account1”

        只需将这行代码替换为以下代码:

        account1 = new BankAccount("","",rate,type);
        

        还在 else if 块中对 account2 进行类似的更改。 你会得到想要的结果

        11.0
        true
        12.0
        true
        false
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-04
          • 1970-01-01
          • 2017-12-19
          • 2021-02-08
          • 2020-07-23
          • 1970-01-01
          • 2011-05-26
          • 1970-01-01
          相关资源
          最近更新 更多