문제

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

    }
}

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

누군가 메시지를 인쇄하는 방법을 말해 줄 수 있습니까? bMethod?

도움이 되었습니까?

해결책

인스턴스화만 할 수 있습니다. MethodLocalInner 이내에 aMethod.그렇습니다

void aMethod() {

    class MethodLocalInner {

            void bMethod() {

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

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

}

다른 팁

메소드 내에서 방법 클래스 선언 후 방법LocalInner 예를 들어 다음 호출을 수행할 수 있습니다.

new MethodLocalInner().bMethod();

그냥 인스턴스를 만들어 보는 게 어때? MethodLocalInner, 안에 aMethod, 그리고 전화 bMethod 새 인스턴스에서?

이렇게 하면 시작할 수 있습니다(테스트할 만한 편리한 항목이 없습니다).수정된 생성자 구문을 확인하세요.

http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

기본 메소드 내에서 new Outer().aMethod()를 호출해야 합니다.또한 다음과 같이 aMethod() 내부에 MethodLocalInner().bMethod()에 대한 참조를 추가해야 합니다.

public class Test {
    public static void main(String[] args) {
        new Outer().aMethod();
    }
}


void aMethod() {
    class MethodLocalInner {
        void bMethod() {
            System.out.println("Inside method-local bMethod");
        }
    }
    new MethodLocalInner().bMethod();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top