【发布时间】: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