【问题标题】:If statement Object boolean not working如果语句对象布尔值不起作用
【发布时间】:2017-07-02 13:56:03
【问题描述】:

我正在尝试使用另一个类中的另一个对象来检查一个人的年龄,出于某种原因,areThey 布尔值无论如何都会返回 false。我实现的以下代码是_

  public class main {    
    public static void main(String[] args){    
      Obj object = new Obj();    
      object.areTheyOldEnough();

      if(object.areTheyOldEnough() == true){
          System.out.println("They are old enough!");
      }else{
          System.out.println("They are not old enough!");
      }
    }
}

public class Obj {
    private int age = 15;
    private boolean areThey;

    public boolean areTheyOldEnough() {
        if (age > 12) {
            boolean areThey = true;
        }
        return areThey;    
    } 
} 

【问题讨论】:

  • 调用Object 类的实例并不是一个好主意。您应该阅读命名约定。
  • 独立你的问题你应该知道:类名应该是名词,大小写混合,每个内部单词的第一个字母大写。 (oracle.com/technetwork/java/codeconventions-135099.html)。我的意思是你应该定义你的类:public class Obj
  • 除此之外if (booleanMethod() == true) 也是不好的做法。你看,if (areTheyOldEnough()) 更容易阅读!

标签: java oop object boolean


【解决方案1】:

你的问题叫做shadowing;这里:

最内在的声明:

if (age > 12) {
  boolean areThey = true;

完全错了,应该这样写:

areThey = true;

相反。关键是:您要声明一个具有该名称的 new 变量,并为其赋值true。但是,当 if 块是“左”时,该变量会消失得无影无踪……并且返回的是您的 obj 类中的 field areThey 的值。而那个仍然的初始默认值为false

除此之外:命名是代码中的一个真正问题。使用 A) 符合 Java 编码标准的名称;例如,类名以大写开头; B) 意思某事。

换句话说:名称 Object 没有任何意义(除了创建另一个名称与 java.lang.Object 类名称冲突)。最好将其称为“testInstance”或类似的名称 - 如前所述:使用意味着某事的名称。

【讨论】:

    【解决方案2】:

    您有两个同名的布尔变量 - 一个是您在方法中设置的局部变量(在 if 块内),另一个是您要返回的实例变量。

    您不需要任何布尔变量,只需编写:

    public boolean areTheyOldEnough() {
        return age > 12;
    }
    

    【讨论】:

      【解决方案3】:

      你混合了字段和局部变量的可见性

      public class obj {
      
      public int age = 15;
      public boolean areThey; //initialized to false as default value for boolean
      
      public boolean areTheyOldEnough() {
         if (age > 12) {
             boolean areThey = true; //create new local variable with the same name as field, 
                                     // but visible just in scope of if-block
          }
          return areThey; // return value from field
      }
      

      【讨论】:

        【解决方案4】:

        更正您的代码如下:

        public class main {
        
        public static void main(String[] args){
        
          obj Object = new obj();
        
          if(obj.areTheyOldEnough()){
              System.out.println("They are old enough!");
          } else{
              System.out.println("They are not old enough!");
          }
        }
        

        你的 obj 类将是:

        public class obj {
        public int age = 15;
        // it is not required, but if you need for other methods,
        // you can define it
        public boolean areThey;
        
        public boolean areTheyOldEnough() {
            return age > 12;
            // or if you need set variable areThey, so use following codes
           /*
              areThey = age > 12;
              return areThey;
           */
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-21
          • 2013-01-31
          • 2016-08-29
          • 2018-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多