public class Fat {
protected String x="hi";
public Fat(String x){
this.x=x;
}
public void aa(){
System.out.println("原来的方法");
}
}
public class Test {
public void hh(){
new Fat("ffff"){
public void aa(){
System.out.println("匿名类,匿名实例的方法");
super.aa();
System.out.println(super.x);
//注意此处的x已经不是超类中的初始值了,被构造方法重新改写过了
}
}.aa(); //当时定义,当时调用
}
public static void main(String[] args){
Test t=new Test();
t.hh();
}
}
再续:匿名内部类调用父类方法或访问父类成员
最新推荐文章于 2024-02-06 11:06:27 发布