Java中的两个布尔变量

发布于 2024-12-05 13:12:08 字数 85 浏览 0 评论 0原文

我想在java中编写两个布尔变量,它们以一种始终为假、一个始终为真的方式相互关联。因此,如果您将其中一项设置为 true,则另一项将自动更改为 false。

I would like to program in java two Boolean variables which are corelated in a way that one is allways false and one allways true. So if you set one to true the other one would automaticly change to false.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

后来的我们 2024-12-12 13:12:08

不要使用变量——使用方法。

在使用变量之前,

class Before {
    boolean first;
    boolean second;

    boolean setFirst(boolean newValue) {
        first = newValue;
        second = !first;
    } 

    boolean setSecond(boolean newValue) {
        second = newValue;
        first = !second;
    }
}

请像这样更好:使用单个数据(这是您真正拥有的)并使用逻辑方法。

class After {
    private boolean value;

    boolean first() {
        return value;
    }

    boolean second() {
        return !value;
    }
}

Don't use variables - use methods.

Before using variables

class Before {
    boolean first;
    boolean second;

    boolean setFirst(boolean newValue) {
        first = newValue;
        second = !first;
    } 

    boolean setSecond(boolean newValue) {
        second = newValue;
        first = !second;
    }
}

make this better like so: use a single piece of data (which is what you really have) and use methods for the logic.

class After {
    private boolean value;

    boolean first() {
        return value;
    }

    boolean second() {
        return !value;
    }
}
夜访吸血鬼 2024-12-12 13:12:08

使用 setter 和 getter 来为您管理逻辑。

class Foo{

    private boolean _bool1;
    private boolean _bool2;

    public void setBool1(boolean value)
    {
       _bool1 = value;
       _bool2 = !value;
    }

    public void setBool2(boolean value)
    {
       _bool2 = value;
       _bool1 = !value;
    }

    public boolean getBool1() { return _bool1 ;}

    public boolean getBool2() { return _bool2 ;}
}

Use setters and getters to manage the logic for you.

class Foo{

    private boolean _bool1;
    private boolean _bool2;

    public void setBool1(boolean value)
    {
       _bool1 = value;
       _bool2 = !value;
    }

    public void setBool2(boolean value)
    {
       _bool2 = value;
       _bool1 = !value;
    }

    public boolean getBool1() { return _bool1 ;}

    public boolean getBool2() { return _bool2 ;}
}
红墙和绿瓦 2024-12-12 13:12:08

使用智能设置器

class Blah {
   private bool1 = true;
   private bool2 = false;


   setBool1(val) {
      this.bool1 = val;
      this.bool2 = !val;
   }

   setBool2(val) {
       this.bool2 = val;
       this.bool1 = !val;
   }

   // more setters/getters

}

注意我不确定您是否真的需要这个。如果 2 个布尔值总是相反,为什么不只使用 1 个布尔值并根据它做出决策,而不是根据 2 个布尔值做出决策?

use smart setters

class Blah {
   private bool1 = true;
   private bool2 = false;


   setBool1(val) {
      this.bool1 = val;
      this.bool2 = !val;
   }

   setBool2(val) {
       this.bool2 = val;
       this.bool1 = !val;
   }

   // more setters/getters

}

note I'm not sure if you really need this. If the 2 booleans are always opposites, why not just have 1 bool and make decisions based on it, instead of making decisions based on 2 bools?

裸钻 2024-12-12 13:12:08

我怀疑你想要 boolean 而不是 Boolean

我也怀疑你只需要一个字段,flag1 和一种方法

 public boolean getFlag2() {
      return !flag1;
 }        

I suspect you want boolean rather than Boolean

I also suspect you only need one field, flag1 with a method

 public boolean getFlag2() {
      return !flag1;
 }        
玩心态 2024-12-12 13:12:08
public class Opposites {
  protected boolean x = true;
  protected boolean y = false;
  public boolean getX() { return x; }
  public boolean getY() { return y; }
  public boolean toggle() { x=!x; y=!y; }
}

Opposites o = new Opposites();
o.getX(); // => true
o.getY(); // => false
o.toggle();
o.getX(); // => false
o.getY(); // => true
public class Opposites {
  protected boolean x = true;
  protected boolean y = false;
  public boolean getX() { return x; }
  public boolean getY() { return y; }
  public boolean toggle() { x=!x; y=!y; }
}

Opposites o = new Opposites();
o.getX(); // => true
o.getY(); // => false
o.toggle();
o.getX(); // => false
o.getY(); // => true
影子的影子 2024-12-12 13:12:08

您只能使用一个布尔变量:

boolean flag = true;
/*
flag is true
!flag is false
*/

...

flag = false;
/*
flag is false
!flag is true
*/

you can use only one boolean variable:

boolean flag = true;
/*
flag is true
!flag is false
*/

...

flag = false;
/*
flag is false
!flag is true
*/
用心笑 2024-12-12 13:12:08

只需使用 !myVar 得到相反的结果,而不是使用两个变量。如果需要,这可以在函数中。

Just get the opposite with !myVar instead of having two variables. This can be in a function if you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文