{
void show() {
final int y = 0;
class Inner extends AbsDemo
{
void show() {
System.out.println("show:" + y);
}
}
}
}
我估计你是想问这个吧。。。这样是不能调用的,需要加final,因为外部类的方法的生命周期比内部类的短,所以需要加上final修饰符作者: Rancho_Gump 时间: 2012-12-5 21:35
内部类的的变量内部类是可以调用 内部类可以直接访问外部类的成员,因为持有外部类中的引用。
但是不可以访问它所在的局部中的变量,只能访问被final修饰的局部变量。
例如:class Demo
{
public static void main(String[] args)
{
Out n =new Out();
n.printOut();
n.show();
}
}
class Out
{
int a = 1;
class In
{
int a=2;
public void print()
{
int a=3;
System.out.println(a);//打印3
System.out.println(this.a);// 打印2
System.out.println(Out.this.a);//打印1
}
}
void show()
{
final int x = 1;// 注意此处不加final修饰,编译失败,毕老师讲的应该是这种情况,你练习时是将x定义在了内部类之中,这样是可以访问的。
class In1
{
void print()
{
System.out.println(x);
}
}
new In1().print();
}
void printOut()
{
In n = new In();
n.print();