我有一个应该返回位置对象的函数,但我还需要测试某些东西的计算结果是否为假,此外,调用者需要知道这两条信息。我的返回类型为 Place 但在 Java 中没有引用参数,因此如果以下 if-condition 为真,我希望以某种方式在调用者中反映它,以便我可以检查它,但我不能不止一种返回类型,所以我不知道该怎么做。我最好的尝试是返回 null,但我只是觉得这是糟糕的编程。
if (directions.get(i).isLocked())
下面是完整的功能:
Place followDirection(String dir, boolean isLocked) {
dir = dir.toLowerCase(); // make sure the string is lowercase for comparisons
int i = 0;
for ( i = 0; i < directions.size(); i++ ) { // loop until we find a match, remember that if it's locked then we cnanot go in there
if ( directions.get(i).getDirection().equals(dir) ) {
if ( directions.get(i).isLocked() ) {
System.out.println("This room is locked, sorry");
}
else {
return directions.get(i).getToPlace(); // this means we found a match, return the destination
}
}
}
Place p = null;
return p;
}
眼眸繁星
叮当猫咪
相关分类