内部类
一、内部类种类
{
1)成员内部类
2)局部内部类
}
二、成员内部类
成员内部类的变量使用
private int num = 20;
class Inner2{
int num = 1;
void method2(){
int num =2;
System.out.println(num);
System.out.println(this.num);
System.out.println(Outer.this.num);
}
}
三、局部内部类的方法使用
在外部类创建新的方法,在方法区内创建局部类(局部类只能用class+类名的形式创建)
public class Outer {
private int num = 20;
void method(){
class Inner {
void method(){
System.out.println(num);
}
}
Inner out = new Inner();
out.method();
}
通过调用外部类的成员方法的方式 间接调用内部类的方法
public class Use {
public static void main(String[] args) {
Outer cc = new Outer();
cc.method();
}
}