【问题标题】:How do I change the value of a boolean within a method - Java?如何更改方法中的布尔值 - Java?
【发布时间】:2014-12-01 01:41:38
【问题描述】:

我在一本书中读到,当您在方法内更改布尔值或其他基本数据类型的方法参数的值时,它只会在方法内更改,而在外部保持不变。我想知道是否有某种方法可以让我在方法中实际更改它。例如:

public class Change {

    void convert(boolean x, boolean y, boolean z) { //i want to set x,y, and z to false in this 
        x = false;
        y = false;
        z = false;
     }

}

//Now in my main class: 
public static void main(String[] args) {
    boolean part1 = true;
    boolean part2 = true;
    boolean part3 = true;
    System.out.println(part1 + " " + part2 + " " + part3);


    Change myChange = new Change();
    myChange.convert(part1,part2,part3);
    System.out.println(part1 + " " + part2 + " " + part3);

}

EDIT1:这些答案很好,但不是我想要实现的。当我调用该方法时,我想放入第 1 部分、第 2 部分和第 3 部分,而不是希望在方法中将它们设置为 false。我问这个问题的具体原因是因为我试图编写一艘战舰,我有一个子程序类,它有一个方法,当它被调用时,它会检查一艘船是否沉没。如果存在接收器,则该方法会将大量布尔变量设置为 false。

EDIT2:澄清一下,我想要这样的东西:

void convert(thing1,thing2,thing3,thing4) {

     //some code here that sets thing1,thing2,thing3, and thing4 to false

 }

 // than in main:
 boolean test1 = true;
 boolean test2 = true;
 boolean test3 = true;
 boolean test4 = true;
 convert(test1,test2,test3,test4);
 System.out.println(test1 + " " + test2 + "....");
 //and that should print out false, false, false, false

【问题讨论】:

    标签: java methods parameters converter return-value


    【解决方案1】:

    你可以用这种方法做到这一点

    // these are called instance variables
    private boolean x = false;
    private boolean y = false;
    private boolean z = false;
    
    // this is a setter
    public static void changeBool(boolean x, boolean y, boolean z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    

    这样调用方法

    changeBool(true, true, false);
    

    现在更改了 x、y 和 z 的值。

    【讨论】:

      【解决方案2】:

      这是 Java 中的一个常见问题——按值传递与按引用传递。 Java 始终是按值传递的,您将其视为按引用传递。

      就像@Rafael 所说,你需要使用实例变量来做你想做的事。我已经更进一步并编辑了您的源代码以执行您想要的操作:

      public class Change {
      boolean part1;
      boolean part2;
      boolean part3;
      
      Change(boolean x, boolean y, boolean z) {
          part1 = x;
          part2 = y;
          part3 = z;
      }
      
      void convert(boolean x, boolean y, boolean z) { //this now sets the class variables to whatever you pass into the method 
          part1 = x;
          part2 = y;
          part3 = z;
      }
      
      // Now in my main class:
      public static void main(String[] args) {
          Change myChange = new Change(true, true, true);
          System.out.println(myChange.part1 + " " + myChange.part2 + " "
                  + myChange.part3);
          myChange.convert(false, false, false);
          System.out.println(myChange.part1 + " " + myChange.part2 + " "
                  + myChange.part3);
      
      }
      }
      

      【讨论】:

      • 谢谢!还有你@Rafael
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多