【问题标题】:Change boolean Values?更改布尔值?
【发布时间】:2014-01-06 00:44:10
【问题描述】:

我对 Java 中的布尔值有疑问。假设我有一个这样的程序:

boolean test = false;
...
foo(test)
foo2(test)

foo(Boolean test){
  test = true;
}
foo2(Boolean test){
  if(test)
   //Doesn't go in here
}

我注意到在 foo2 中,布尔测试没有改变,因此不会进入 if 语句。那我怎么改呢?我查看了布尔值,但找不到将测试从真“设置”为假的函数。如果有人可以帮助我,那就太好了。

【问题讨论】:

  • Java 是按值传递的。使变量成为实例变量并修改并检查它。
  • @SotiriosDelimanolis 是的。但是 java.lang.Object(和子类型,即任何非原始类型)的值是引用地址。
  • @ElliottFrisch 我看不到 but 的来源。
  • @SotiriosDelimanolis 对不起。 并且 java.lang.Object(和子类型,即任何非原始类型)的值是引用地址。

标签: java boolean primitive-types


【解决方案1】:

这里有一个很好的解释。

http://www.javadude.com/articles/passbyvalue.htm

Java有指针,传入的是指针的值。有 无法将对象本身作为参数实际传递。你只能 将指针(值)传递给对象。

还有我的解决方案

   public static class MutableBoolean {
        public boolean value;

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

用法:

MutableBoolean needStop = new MutableBoolean(false);
call( new Listener(needStop){
   void onCallback(){
      needStop.value = true;
   }

})

【讨论】:

  • 仅供参考 org.apache.commons.lang3.mutable.MutableBoolean - Appache Common Lang 中已经存在
【解决方案2】:

您需要注意:

  1. 在 Java 中,参数是按值传递的。
  2. 布尔值,布尔值的包装类型,是不可变的。

因为1和2,你无法在方法中改变布尔传递的状态。

你通常有两个选择:

选择 1: 有一个可变的布尔值持有者,例如:

class BooleanHolder {
   public boolean value;  // better make getter/setter/ctor for this, just to demonstrate
}

所以在你的代码中应该是这样的:

void foo(BooleanHolder test) {
  test.value=true;
}

选择2:更合理的选择:从你的方法中返回值:

boolean foo(boolean test) {
  return true;   // or you may do something else base on test or other states
}

调用者应该像这样使用它:

boolean value= false;
value = foo(value);
foo2(value);

这种方法更受欢迎,因为它更适合普通的 Java 编码实践,并且通过方法签名它向调用者提示它将根据您的输入返回一个新值

【讨论】:

    【解决方案3】:

    您的foo 方法将test 的值更改为true。看起来你想要的是为每个函数使用实例变量。

    boolean test = false;
    ...
    foo(test)
    foo2(test)
    
    foo(Boolean test){
      this.test = true;
    }
    foo2(Boolean test){
      if(this.test)
       //Doesn't go in here
    }
    

    这样,您的方法仅更改该方法内部的test 的值,但您的公共test 参数保持false 值。

    【讨论】:

      【解决方案4】:

      您将原始布尔值传递给您的函数,没有“引用”。因此,您只是在 foo 方法中隐藏值。相反,您可能想要使用以下之一 -

      持有人

      public static class BooleanHolder {
        public Boolean value;
      }
      
      private static void foo(BooleanHolder test) {
        test.value = true;
      }
      
      private static void foo2(BooleanHolder test) {
        if (test.value)
          System.out.println("In test");
        else
          System.out.println("in else");
      }
      
      public static void main(String[] args) {
        BooleanHolder test = new BooleanHolder();
        test.value = false;
        foo(test);
        foo2(test);
      }
      

      输出“测试中”。

      或者,通过使用

      成员变量

      private boolean value = false;
      
      public void foo() {
        this.value = true;
      }
      
      public void foo2() {
        if (this.value)
          System.out.println("In test");
        else
          System.out.println("in else");
      }
      
      public static void main(String[] args) {
        BooleanQuestion b = new BooleanQuestion();
        b.foo();
        b.foo2();
      }
      

      其中,也输出“In test”。

      【讨论】:

      • 使用 Holder 方法,您是否需要创建一个名为 BooleanHolder 的新文件,因为它是公开的?
      • 我用Nested Class 编写了它,但你可以(但它不会是静态嵌套类)。
      【解决方案5】:

      您将参数命名为与实例变量相同的名称。在这里,参数是引用的,而不是实例变量。这称为“遮蔽”,其中简单名称 test 作为参数名称遮蔽了也称为 test 的实例变量。

      foo 中,您将参数test 更改为true,而不是未更改的实例变量test。这就解释了为什么它没有进入foo2 中的if 块。

      要赋值,去掉foo上的参数,或者使用this.test引用实例变量。

      this.test = true;
      

      if (this.test)
      

      【讨论】:

      • 嗯,用这个方法我得到一个错误说:错误:非静态变量,这不能从静态上下文中引用
      猜你喜欢
      • 1970-01-01
      • 2016-07-11
      • 2012-12-30
      • 1970-01-01
      • 2012-10-29
      • 2012-03-25
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多