【问题标题】:Passing boolean argument in method but method boolean variable is still unused在方法中传递布尔参数,但方法布尔变量仍未使用
【发布时间】:2017-12-01 18:23:58
【问题描述】:

这里有点 Java 菜鸟。所以我调用了一个名为ageRestrProcessor() 的方法,它接受一个布尔值ageValidationStatus 参数到Processing 类中。 Processing 类有一个布尔变量ageNotValidated 已经初始化为true。所以现在,我尝试将ageNotValidated 变量作为参数传递给ageRestrProcessor() 方法,然后在方法中尝试将布尔值更改为false。但是,在方法本身中,它表示布尔变量 ageValidationStatus 未使用。很明显,布尔值永远不会改变。从表面上看,这似乎应该可行,但我无法弄清楚问题所在。提前感谢您的回复。

//ItemProcessors class

public class ItemsProcessors {
  void ageRestrProcessor(int keyNum, String itemNameFromDB, double itemUnitPriceFromDB, boolean ageValidationStatus, boolean ageValidator, String itemUnit, Map<Integer, Object[]> map){
    double ageRstrItemPrice = bp.roundUpToTwoDecimals(itemUnitPriceFromDB);
    String dollarAgeRestPrice = bp.addDollarFormat(ageRstrItemPrice);

    if (ageValidator){
        System.out.println("\tAge Validation Successful");
        System.out.println("\tTotal Price: " + dollarAgeRestPrice);
        map.put(keyNum, new Object[] {itemNameFromDB, itemUnitPriceFromDB, itemUnit, ageRstrItemPrice, dollarAgeRestPrice});

        //Here, the compiler is telling me that ageNotValidated variable is never used.
        ageValidationStatus = false;
    }
    else {
        System.out.println("\tShopper is underage. Item not added");
    }
  }
}

//I'm calling the ageRestrProcessor method into this Processing class

public class Processing extends OrderData {
   private Map<Integer, Object[]> itemMap = new HashMap<Integer, Object[]>();
   BasePage bp = new BasePage();
   private Exceptions xcep = new Exceptions();
   private ItemsProcessors ip = new ItemsProcessors();
   public boolean ageNotValidated = true;

   public void itemsProcessor(XSSFSheet itemSheet){
      if (xcep.isAgeRestricted(itemSheet, rowNum) && ageNotValidated){
         String ageEntered = bp.getStringFromScanner("\tEnter DOB in 'mm-dd-yyyy' format: ");
         boolean isAgeValid = xcep.ageValidator(ageEntered);

         //I'm trying to pass the ageNotValidated variable into the method here.
         ip.ageRestrProcessor(keynum++, itemNameFromDB, itemUnitPriceFromDB, ageNotValidated, isAgeValid, itemUnit, itemMap);
      }
    }
}

【问题讨论】:

    标签: java boolean arguments encapsulation


    【解决方案1】:

    我认为您可能会混淆传递值 value 和传递引用。默认情况下,Java 中的原始类型是按值传递的。

    这意味着,制作了一个副本。在方法中更改按值参数的值只会更改副本,不会更改用于传递它的变量。

    传值时,参数不需要是变量。你可以传入文字值:

    myfunction(false);
    

    此外,设置变量的值不会“使用”该变量。您可以设置它,但如果它从未被读取,则它被认为是“死代码”,因为它没有效果。因此,您会收到警告。

    在您的场景中,我建议让 ageRestrProcessor() 返回一个布尔值以指示是否验证了年龄。调用者可以相应地更改原始布尔值。

    【讨论】:

    • 在 java 中,没有通过引用传递原始类型的机制。您需要将其封装在一个对象中。
    【解决方案2】:

    ageValidationStatus 是在 ageRestrProcessor 方法内创建的,不能在此方法外使用(local 变量),并且在当前方法的其他任何地方也没有使用它

    所以这是编译器的警告,以提高代码质量,当它在其他地方没有用时进行不必要的赋值


    使ageRestrProcessor返回boolean标志并相应地使用值

      boolean ageRestrProcessor(int keyNum, String itemNameFromDB, double itemUnitPriceFromDB, boolean ageValidationStatus, boolean ageValidator, String itemUnit, Map<Integer, Object[]> map){
             //..code
             // you can remove  boolean ageValidationStatus from method signature
        if (ageValidator){
             //..code
             return false;
        }
        else {
            System.out.println("\tShopper is underage. Item not added");
            return true;
        }
      }
    

    ageNotValidated = ip.ageRestrProcessor(keynum++,....;
    

    【讨论】:

    • 感谢@Pavneet_Singh!我让该方法返回一个布尔值,然后根据需要设置值,一切都很好!感谢您的指导!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    相关资源
    最近更新 更多