Java中的可变布尔字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1385481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:27:52  来源:igfitidea点击:

Mutable boolean field in Java

java

提问by javapowered

I need a mutable boolean field in Java (I will return this field via get* method later and it should be possible to modify this field).

我需要一个 Java 中的可变布尔字段(稍后我将通过 get* 方法返回该字段,并且应该可以修改该字段)。

Boolean doesn't work because there are no set* methods in the Boolean class (I would say that Boolean is immutable, you can only change the reference, but you can't change the object itself).

Boolean 不起作用,因为 Boolean 类中没有 set* 方法(我会说 Boolean 是不可变的,您只能更改引用,而不能更改对象本身)。

I guess I can use Boolean array of size 1. But probably there are more elegant solutions?

我想我可以使用大小为 1 的布尔数组。但可能有更优雅的解决方案?

Why doesn't Java have such a simple thing?

为什么Java没有这么简单的东西?

采纳答案by Thomas Jung

Immutable classes are easier to work with. They'll never change and there will be no problems with concurrent code. (Basically, there are fewer possibilities to break them.)

不可变类更容易使用。它们永远不会改变,并发代码也不会有问题。(基本上,破坏它们的可能性较小。)

If you would like to return a reference to your Boolean value, you can use java.util.concurrent.atomic.AtomicBooleanif you're working with multiple threads or plain old org.apache.commons.lang.mutable.MutableBoolean.

如果您想返回对布尔值的引用,您可以使用java.util.concurrent.atomic.AtomicBoolean多线程或普通的 old org.apache.commons.lang.mutable.MutableBoolean

回答by Brian Agnew

Why not use the boolean primitive ?

为什么不使用布尔原语?

e.g.

例如

private boolean myFlag = false;

public void setMyFlag(boolean flag) {
   myFlag = flag;
}

Note your getter method can return a Boolean if required, due to the magic of autoboxing. This allows easy interchangeability between using primitives and their object equivalents (e.g. boolean vs. Boolean, or int vs. Integer).

请注意,由于autoboxing的魔力,如果需要,您的 getter 方法可以返回一个布尔值。这允许在使用原语和它们的对象等价物(例如布尔值与布尔值,或整数与整数)之间轻松互换。

So to address your edited responses re. the methods you have available,

因此,要解决您编辑后的回复。你可用的方法,

public Object getAttribute(String attributeName)

can be implemented by returning an autoboxed boolean,.

可以通过返回一个自动装箱的布尔值来实现。

回答by Tom Jefferys

What about just using the boolean primitive?

只使用布尔原语怎么样?

private boolean value;

public void setValue(boolean value) {
    this.value = value;
}

public boolean getValue() {
    return value;
}

回答by Adam Batkin

Maybe write yourself a wrapper class

也许自己写一个包装类

class BooleanHolder {
    public boolean value;
}

Or make a generic holder class (which means you will have to use class Booleaninstead of primitive boolean):

或者创建一个通用的持有者类(这意味着你将不得不使用 classBoolean而不是primitive boolean):

class Holder<T> {
    public T value;
}

You then return the wrapper class instead of the value itself, which allows the value inside the wrapper to be modified.

然后返回包装器类而不是值本身,这允许修改包装器内的值。

回答by CPerkins

Are you really saying that you want callers to be able to modify the object's boolean value by manipulating what gets returned? So that the object and caller would share a reference to it?

您真的是说您希望调用者能够通过操作返回的内容来修改对象的布尔值吗?以便对象和调用者共享对它的引用?

Just so I understand, given:

就我所知,鉴于:

class OddClass {
   private Boolean strangeFlag;
   public Object getAttrbiute(String attributeName) { 
      if (attributeName.equals("strangeflag")) return (Object)strangeFlag; 
      ...
   }
}

And then caller does:

然后调用者做:

   Boolean manipulableFlag = (Boolean) myOddClass.getAttrbiute ("strangeflag");

And then later, if caller changes the value of manipulableFlag, you want that change to happen in the OddClass instance, just as though caller had instead used a setAttrbiute method.

然后,如果调用者更改了 的值manipulableFlag,您希望该更改发生在 OddClass 实例中,就像调用者使用 setAttrbiute 方法一样。

Is that what you're asking?

这就是你要问的吗?

In that case, you'd need a holder class, as suggested by Adam.

在这种情况下,您需要一个持有者类,正如亚当所建议的那样。

回答by CPerkins

If you are using Java 5 or higher then use AtomicBoolean

如果您使用的是 Java 5 或更高版本,请使用AtomicBoolean

回答by user2218625

You can use a boolean array

您可以使用布尔数组

final boolean[] values = { false };
values[0] = true;

回答by vonWippersnap

If you are using Android, you can use the android.util.Mutable* objects which wrap various primitive values. For example, quoting from the SDK source:

如果您使用的是 Android,则可以使用包装各种原始值的 android.util.Mutable* 对象。比如引用SDK源码:

public final class MutableBoolean {
  public boolean value;

  public MutableBoolean(boolean value) {
      this.value = value;
  }
}

回答by Lukas

The answer I liked most was from Adam to write your own wrapper class... OK

我最喜欢的答案是 Adam 写的自己的包装类……好吧

/* Boolean to be passed as reference parameter */
public class Bool {

     private boolean value;

     public Bool() {
         this.value = false;
     }

     public boolean is() {
         return this.value;
     }

     public void setTrue() {
         this.value = true;
     }

     public void setFalse() {
         this.value = false;
     }
 }