java内部类细节

 1 package face_09;
 2 /*
 3  * 为什么内部类能直接访问外部类中的成员呢?
 4  * 那是因为内部类持有了外部类的引用。 外部类名.this
 5  * 
 6  */
 7 class Outer {
 8     int num = 3;
 9     class Inner {
10         int num = 4;
11         void show() {
12             int num = 5;
13             System.out.println(num);//Outer.this.num 打印3   Inner.this.num  打印4
14         }
15     }
16     void method() {
17         new Inner().show();
18     }
19 }
20 public class InnerClassDemo2 {
21     public static void main(String[] args) {
22         new Outer().method();
23     }
24 }
View Code

 

posted @ 2021-10-06 21:12  doremi429  阅读(19)  评论(0)    收藏  举报