2008-09-17 59 views
2
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消息?方法本地内部类

回答

6

您只能在aMethod实例MethodLocalInner。所以,做

void aMethod() { 

    class MethodLocalInner { 

      void bMethod() { 

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

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

} 
+0

Thanks..realised我哪里wrong..I把新实例行localinner课前创建。 – Omnipotent 2008-09-17 07:00:52

1

在该方法的声明后amethod方法 MethodLocalInner你可以例如做以下电话:

new MethodLocalInner().bMethod(); 
1

你为什么不只是创建一个实例MethodLocalInner,在aMethod,并且在新实例上调用bMethod

0

您需要在主方法内调用新的Outer()。aMethod()。您还需要添加到MethodLocalInner()的参考bMethod()您的amethod方法(内),这样的:

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(); 
}