public class Chinese extends Person{
public void print(){
System.out.println("Chinese:");
super.print(); //调用父类的print
}
public static void main(String[] args){
Chinese chn=new Chinese();
chn.setName("kiweis");
chn.setAge(18); //chn的age数据已设置成18
chn.print();
System.out.println();
}
}
class Person{
public int c;
private String name;
private int age;
protected void setName(String name){
this.name=name;
}
protected void setAge(int age){
this.age=age;
}
protected void print(){
System.out.println("Name="+name+" Age="+age); /*子类通过super来调用,打印的究竟是子类的name还是父类的name呢?*/
}
}
以上定义的Chinese继承Person,我的理解是Chinese继承了Person的数据域和方法,调用set方法设置的不是Chinese的String和age么,那么super.print()打印的不是Person的String和age吗,也就是null和0,为什么运行程序输出的是kiweis和18