package com.zy.jczyw.testcase;
class Outer{
private int x = 3;
class Inner{
int x = 4;
void function (){
int x = 5;
System.out.println(x); //5
System.out.println(this.x); //4
System.out.println(Outer.this.x); //3
}
}
void method(){
Inner in = new Inner();
in.function();
}
}
public class InnerDemo{
public static void main(String[] args) {
Outer out = new Outer();
out.method();
Outer.Inner inner = new Outer().new Inner();
inner.function();
}
}
內部類訪問規則:
1,內部類可以直接訪問外部類中的成員,包括私有,原因:內部類中持有一個外部類的引用,格式:外部類名.this
2,外部類要訪問內部類,必須建立內部類對象