abstract class AbsDemo
{
abstract void show();
}
class Outer
{
int x = 3;
public void function()
{
//父类引用 创建匿名内部类对象
AbsDemo d =new AbsDemo()
{
int x = 9;
void show()
{
int x = 11;
System.out.println("x1== "+x); //x1== 11
System.out.println("x2== "+this.x);//x2== 9
System.out.println("x3== "+Outer.this.x);//int x = 3; }
void abc()
{
System.out.println("hhee");
}
};
d.show();
//调用abc(),是子类中特有的方法,只能用子类对象去调用。
new AbsDemo()
{
int x = 9;
void show()
{
int x = 11;
System.out.println("x1== "+x); //x1== 11
System.out.println("x2== "+this.x);//x2== 9
System.out.println("x3== "+Outer.this.x);//int x = 3;
}
void abc()
{
System.out.println("hhee");
}
}.abc();
}
}
class Test
{
public static void main(String[] args)
{
new Outer().function();
}
}
|