abstract class Outer{
int num;
public Outer(int x){
num = x;
}
public abstract void show1();
public abstract void show2();
}
public class PC{
public static void main(String[] args){
new Outer(55)//构造父类部分
//子类重写部分
{
public void show1(){
System.out.println(num++);
}
public void show2(){
System.out.println(num++);
}
public void show3(){
System.out.println(num++);
}
}.show3();
Outer out = new Outer(55){
public void show1(){
System.out.println(num++);
}
public void show2(){
System.out.println(num++);
}
public void show3(){
System.out.println(num++);
}
};
out.show1();
out.show2();
//out.show3();// 错误: 找不到符号 out.show3();
//注意: 第一个 new Outer(55) 得到是 Outer 的子类对象!
//第二个 new Outer(55)得到子类对象之后又进行了类型提升, 所以它会隐藏自身特有的方法,呈现其父类的属性!
}
}
时间: 2024-10-09 17:37:58