java 上溯_java的继承与上溯

java的继承与上溯

整理了下代码,这样看的会更清楚了。如下;

Java代码

class Parent {

int i = 1;

String j = "Parent J";

StringBuffer k = new StringBuffer("Parent:");

public String getJ() {

return j;

}

public int getI() {

return i;

}

public void printBuffer() {

k.append("Parent Buffer;");

System.out.println(k);

}

static String getClassName() {

return "Parent";

}

}

class Child extends Parent {

int i = 2;

String j = "Child J";

StringBuffer k = new StringBuffer("Child:");

public String getJ() {

return j;

}

public int getI() {

return -i;

}

public void printBuffer() {

k.append("Child Buffer;");

System.out.println(k);

}

static String getClassName() {

return "Child";

}

}

public class ExtendDemo {

public static void main(String[] args) {

Child child = new Child();

System.out.println("Child type test:");

System.out.println("child.i:" + child.i);

System.out.println("child.getI():" + child.getI());

System.out.println("child.j:" + child.j);

System.out.println("child.getJ():" + child.getJ());

child.printBuffer();

System.out.println("child.getClassName():" + child.getClassName());

System.out.println();

Parent parent = child;

System.out.println("Parent type test:");

System.out.println("parent.i:" + parent.i);

System.out.println("parent.getI():" + parent.getI());

System.out.println("parent.j:" + parent.j);

System.out.println("parent.getJ():" + parent.getJ());

parent.printBuffer();

System.out.println("parent.getClassName():" + parent.getClassName());

}

}

class Parent {

int i = 1;

String j = "Parent J";

StringBuffer k = new StringBuffer("Parent:");

public String getJ() {

return j;

}

public int getI() {

return i;

}

public void printBuffer() {

k.append("Parent Buffer;");

System.out.println(k);

}

static String getClassName() {

return "Parent";

}

}

class Child extends Parent {

int i = 2;

String j = "Child J";

StringBuffer k = new StringBuffer("Child:");

public String getJ() {

return j;

}

public int getI() {

return -i;

}

public void printBuffer() {

k.append("Child Buffer;");

System.out.println(k);

}

static String getClassName() {

return "Child";

}

}

public class ExtendDemo {

public static void main(String[] args) {

Child child = new Child();

System.out.println("Child type test:");

System.out.println("child.i:" + child.i);

System.out.println("child.getI():" + child.getI());

System.out.println("child.j:" + child.j);

System.out.println("child.getJ():" + child.getJ());

child.printBuffer();

System.out.println("child.getClassName():" + child.getClassName());

System.out.println();

Parent parent = child;

System.out.println("Parent type test:");

System.out.println("parent.i:" + parent.i);

System.out.println("parent.getI():" + parent.getI());

System.out.println("parent.j:" + parent.j);

System.out.println("parent.getJ():" + parent.getJ());

parent.printBuffer();

System.out.println("parent.getClassName():" + parent.getClassName());

}

}

运行结果如下:

Child type test:

child.i:2

child.getI():-2

child.j:Child J

child.getJ():Child J

Child:Child Buffer;

child.getClassName():Child

Parent type test:

parent.i:1

parent.getI():-2

parent.j:Parent J

parent.getJ():Child J

Child:Child Buffer;Child Buffer;

parent.getClassName():Parent

Child继承Parent,子类的变量和静态方法分别隐藏父类的变量和静态方法,子类的实例方法覆盖父类的实例方法。隐藏只是把父类的东东藏起来,但是其还是实质存在的;而覆盖就是把父类的东东完全抹掉以替换成子类的,是不可恢复的。在child被强制转换成Parent类型后,被隐藏的东西又被恢复了,而被覆盖的方法却是一去不复返了(子类的方法在这个实例里面永久替代了原先父类的方法)。这就是区别,也是这个题的考点所在了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值