- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestInnerclass.java
More file actions
Latest commit
48 lines (46 loc) · 1.06 KB
/
Copy pathTestInnerclass.java
File metadata and controls
48 lines (46 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
内部类
外部类对内部类完全可见,内部内对外部类隐藏细节。
内部类可以用private,protected,default修饰
内部类的变量查找规则:
局域变量->内部类成员->外部类成员->编译出错(都找不到该变量)
用this调用同名变量。
规则:
1.内部类可以直接读取外部类函数,
非静态内部类:
2.非静态内部类不能出现任何静态的东东,包括成员,方法,初始化块。
3.外部类静态方法不能访问非静态内部类的内容。
4.外部类需要创建 非静态内部类实例变量 才能读取内部类的内容。
静态内部类
4.静态内部类可以包含静态和非静态成员,但该内部类还是几声于外部类
5.静态内部类不可以访问任何外部类的实例成员或方法。
6.外部类可以通过静态内部类类名或其对象调用内部方法。
*/
classA
{
inti = 5;
publicvoidshow()
{
System.out.println("这是外部类的数据:");
System.out.println("i="+i);
newB().showInner();
}
classB//B是A的内部类,仅对A可见
{
inti = 10;
intj = A.this.i;
publicvoidshowInner()
{
System.out.println("这是内部类数据:");
System.out.println("i="+i+" "+"j="+j);
}
}
}
classTestInnerclass
{
publicstaticvoidmain(String[] args)
{
Aaa = newA();
aa.show();
}
}