class Inner {
private int age = 13;
public void print() {
int age = 14;
System.out.println("局部变量:" + age);
System.out.println("内部类变量:" + this.age);
System.out.println("外部类变量:" + Outter.this.age);
}
}
}
public class test1 {
public static void main(String[] args) {
Outter out = new Outter();
Outter.Inner in = out.new Inner();
in.print();
}
}
运行结果:
public void Print(final int x) { //这里局部变量x必须设置为final类型!
class Inner {
public void inPrint() {
System.out.println(x);
System.out.println(age);
}
}
new Inner().inPrint();
}
}
public class test1 {
public static void main(String[] args) {
Outter out = new Outter();
out.Print(10);
}
}
运行结果:
class Outter {
private int age = 12;
public void Print(final boolean x) { //这里局部变量x必须设置为final类型!
if(x){
class Inner {
public void inPrint() {
System.out.println(age);
}
}
new Inner().inPrint();
}
}
}
public class test1 {
public static void main(String[] args) {
Outter out = new Outter();
out.Print(true);
}
}
运行结果:12