方法局部内部类

Omn*_*ent 2 java inner-classes method-invocation

public class Test {
    public static void main(String[] args) {

    }
}

class Outer {
    void aMethod() {
        class MethodLocalInner {
            void bMethod() {
                System.out.println("Inside method-local bMethod");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人能告诉我如何打印消息bMethod吗?

小智 6

您只能实例MethodLocalInneraMethod.那样做

void aMethod() {

    class MethodLocalInner {

            void bMethod() {

                    System.out.println("Inside method-local bMethod");
            }
    }

    MethodLocalInner foo = new MethodLocalInner(); // Default Constructor
    foo.bMethod();

}
Run Code Online (Sandbox Code Playgroud)