class Father{
int i = 5;
public void p(){
i++;
System.out.println(i);
}
}
class Son extends Father{
int i=3;
public void p(){
i++;
System.out.println(i);
}
}
public class Demo4{
public static void main(String[] args){
Father father = new Father();
father.p();
System.out.println(father.i);
Son son = new Son();
son.p();
System.out.println(son.i);
Father fa_so = new Son();
fa_so.p();
System.out.println(fa_so.i);
}
int i = 5;
public void p(){
i++;
System.out.println(i);
}
}
class Son extends Father{
int i=3;
public void p(){
i++;
System.out.println(i);
}
}
public class Demo4{
public static void main(String[] args){
Father father = new Father();
father.p();
System.out.println(father.i);
Son son = new Son();
son.p();
System.out.println(son.i);
Father fa_so = new Son();
fa_so.p();
System.out.println(fa_so.i);
}
}
//Father fa_so = new Son();这样定义对象,以我现在的理解是:要单独操作成员变量,那么看父类的;
如果要调用方法,那么选择子类的成员方法。
等老师讲完这一部分,如果和我理解的不一样,那我会在后面说明。
本文通过一个具体的Java程序示例,展示了父类与子类之间的继承关系。解释了如何在子类中重写方法以及如何调用父类和子类的方法与属性。特别关注了对象类型与引用类型不同时的行为差异。
2667

被折叠的 条评论
为什么被折叠?



