方法中的内部类能不能访问方法中的局部变量?这个问题我验证的怎么就是能访问呢?求大神解惑
public class Test {
public static void main(String[] args)throws Exception{
Outer outer = new Outer();
//获取内部类对象
Object object = outer.outerfuc();
Method method = object.getClass().getMethod("innerfuc");
method.invoke(object);
}
class Outer {
public Object outerfuc(){
int x = 6;
class Inner{
public void innerfuc(){
System.out.println(x);
}
}
Inner inner = new Inner();
inner.innerfuc();
return inner;
}
}
} |
|