下面程序,为什么向上转型结果输出sub.getField()=1 而不是0或者sub.field=0而不是1 求解释???
public class Super {
public int field=0;
public int getField(){
return this.field;
}
}
public class Sub extends Super{
public int field=1;
public int getSuperField(){
return super.field;
}
public int getField() {
return this.field;
}
}
public static void main(String[] args) {
Super sub=new Sub();
System.out.println("向上转型结果如下");
System.out.println("sub.field="+sub.field);
System.out.println("sub.getField()="+sub.getField());
Sub sub1=new Sub();
System.out.println("向下转型结果如下");
System.out.println("sub1.field="+sub1.field);
System.out.println("sub1.getField()="+sub1.getField());
System.out.println("sub1.getSuperField()="+sub1.getSuperField());
}
运行结果:
向上转型结果如下
sub.field=0
sub.getField()=1
向下转型结果如下
sub1.field=1
sub1.getField()=1
sub1.getSuperField()=0
|