- class Demo1_InnerClass {
- public static void main(String[] args) {
- //调用局部内部类方法,在局部方法里创建内部类对象
- Outer ou = new Outer();
- //ou.method();
-
- //调用私有内部类,创建内部类对象
- //ou.getIn();
- //静态内部类
- Outer.Inner in = new Outer.Inner();
- in.method();
- }
- }
- /*
- //局部内部类
- class Outer {
- private String str = "We will not use 'HelloWorld'!";
- public void method() {
- class Inner {
- public void method() {
- System.out.println("Hello World"+'\n'+str);
- }
- }
- Inner in = new Inner();
- in.method();
- }
- }
- */
- //私有内部类
- /*
- class Outer {
- private String str = "We will not use 'HelloWorld'!";
- private class Inner {
- void method() {
- System.out.println(str);
- }
- }
- public void getIn() {
- Inner in = new Inner();
- in.method();
- }
- }
- */
- //静态内部类
- /**/
- class Outer {
- private String str = "We will not use 'HelloWorld'!";
- static class Inner {
- public void method() {
- System.out.println("Hello World");
- }
- }
- }
复制代码 |
|