在方法中传递布尔参数但方法布尔变量仍未使用

Passing boolean argument in method but method boolean variable is still unused

这里有点 Java 菜鸟。因此,我正在调用一个名为 ageRestrProcessor() 的方法,该方法采用布尔值 ageValidationStatus 参数,进入处理 class。 Processing class 有一个布尔变量 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);
      }
    }
}

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

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


制作 ageRestrProcessor return 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++,....;

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

也就是说,制作了一个副本。在方法中更改按值参数的值只会更改副本,不会更改用于传递它的变量。

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

myfunction(false);

此外,设置变量的值不会 "use" 变量。您可以设置它,但如果它从未被读取,那么它会被视为 "dead code",因为它没有任何作用。因此,您会收到警告。

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