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

时间:2017-12-01 18:23:58

标签: java boolean arguments encapsulation

这里有一点Java noob。所以我调用一个名为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);
      }
    }
}

2 个答案:

答案 0 :(得分: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++,....;

答案 1 :(得分:1)

我认为你可能会混淆传递值并通过引用传递。默认情况下,Java中的基本类型按值传递。

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

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

{% if categories %}
<div class="container">
  <nav id="menu" class="navbar">
    <div class="navbar-header"><span id="category" class="visible-xs">{{ text_category }}</span>
      <button type="button" class="btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"><i class="fa fa-bars"></i></button>
    </div>
    <div class="collapse navbar-collapse navbar-ex1-collapse">
      <ul class="nav navbar-nav">
        {% for category in categories %}
        {% if category.children %}
        <li class="dropdown"><a href="{{ category.href }}" class="dropdown-toggle" data-toggle="dropdown">{{ category.name }}</a>
          <div class="dropdown-menu">
            <div class="dropdown-inner"> {% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}
              <ul class="list-unstyled">
                {% for child in children %}
                    <li><a href="{{ child.href }}">{{ child.name }}</a>
                        {% if child.children %}
                            <ul>
                                {% for children in child.children %}
                                    <li><a href="{{ children.href }}">{{ children.name }}</a></li>
                                {% endfor %}
                            </ul>
                        {% endif %}
                    </li>
                {% endfor %}
              </ul>
              {% endfor %}</div>
            <a href="{{ category.href }}" class="see-all">{{ text_all }} {{ category.name }}</a> </div>
        </li>
        {% else %}
        <li><a href="{{ category.href }}">{{ category.name }}</a></li>
        {% endif %}
        {% endfor %}
      </ul>
    </div>
  </nav>
</div>
{% endif %} 

另外,设置变量的值不会&#34;使用&#34;变量。你可以设置它,但是如果它从未被读过那么它被认为是#34;死代码&#34;因为它没有效果。因此,你会收到警告。

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