局部内部类

来源:互联网 发布:淘宝网密码了怎么办 编辑:程序博客网 时间:2024/05/21 16:31
package com.cn.test;
//局部内部类,可以直接访问外部类的成员
//成员内部类,可以访问外部类的成员,包括私有的
class outer{
private int num=10;
public void method(){
final int num2=20;//访问方法的时候,方法的局部变量会消失,但是方法不会立刻消失,
//而方法内用到了num2,所以要用final
class Inner{

public void show(){
System.out.println(num);
System.out.println(num2);
}
}
//可以创建内部类的对象,通过对象可以使用内部类的一些方法
Inner i=new Inner();
i.show();
}
}
public class InnerPosition {
public static void main(String [] args){
outer o=new outer();
o.method();
}


}
局部类访问自身局部变量的注意事项
//内部类访问本地变量,需要被声明为最终该类型final
0 0
原创粉丝点击