0

我是 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();

    }
}
4

1 回答 1

8

这是有效的,因为该方法innerClassfoo返回一个boolean值,从而满足while条件。顺便说一句,代码应该是这样的:

class InnerDemoClass{               

    boolean innerDemoClassfoo(){
        //implementation goes here
        //make sure to ALWAYS return the value
        boolean result = ...
        return result;
    }
}
于 2013-10-11T19:39:39.507 回答