我是Java的新手,遇到了一个带有'while'循环的程序,该循环的条件是对象引用/调用。我彻底跟踪了代码并没有看到对象的任何终止条件(如果while循环条件中的对象具有'while'循环实现引起的终止条件,那将是有意义的)。
注意:这涉及'内部'/'嵌套'类,所以我甚至不确定这是否有效。如何通过这种语法来满足循环?我将尝试在下面的伪代码中构建我的问题:
class DemoClass {
demoClassfoo() {
InnerClassdemo object2 = this.new InnerDemoClass;
// Here is where my confusion is. I always thought that the
// while loop had to terminate with a logical statement being
// satisfied: like it is calling an object?
while(object2.innerClassfoo()) {
IMPLEMENTATIONS;
}
}
class InnerDemoClass {
innerDemoClassfoo() {
IMPLEMENTATION;
}
}
public static void main(String[] args) {
DemoClass object = new DemoClass();
}
}
答案 0 :(得分:8)
这是有效的,因为方法innerClassfoo
返回boolean
值,从而满足while
条件。顺便说一句,代码应如下所示:
class InnerDemoClass{
boolean innerDemoClassfoo(){
//implementation goes here
//make sure to ALWAYS return the value
boolean result = ...
return result;
}
}