为什么这段代码不调用子类?(Why this code doesn't call the subclass? Inheritance in Java)

编程入门 行业动态 更新时间:2024-10-28 19:26:01
为什么这段代码不调用子类?(Why this code doesn't call the subclass? Inheritance in Java) public class Parent { public void printParent() { System.out.println("I am the Parent"); System.out.println("----this is ::---" + this); this.printChild(); } private void printChild() { System.out.println("This is my child"); } } public class Child extends Parent { private void printChild() { System.out.println("I am the child"); } } public class RelationshipTester { @Test public void testRelation() { Child parent = new Child(); parent.printParent(); } }

这是输出: -

我是家长

----这是:: --- datastructures.lists.inheritance.Child@1a692dec

这是我的孩子

该对象属于Child类型,但它不调用子方法和父方法。 我给了this.printChild();

public class Parent { public void printParent() { System.out.println("I am the Parent"); System.out.println("----this is ::---" + this); this.printChild(); } private void printChild() { System.out.println("This is my child"); } } public class Child extends Parent { private void printChild() { System.out.println("I am the child"); } } public class RelationshipTester { @Test public void testRelation() { Child parent = new Child(); parent.printParent(); } }

This is the output :-

I am the Parent

----this is ::---datastructures.lists.inheritance.Child@1a692dec

This is my child

The object is of the type Child , yet it doesn't call the child method and the parent one. I have given this.printChild();

最满意答案

在Parent类中,您已将printChild声明为private ...并将其命名。 无法覆盖私有方法。 Parent类不知道Child类中的printChild 。

如果您要将private修饰符更改为public ,那么您有一个覆盖,并且您的示例应该输出您期望的内容。


为什么Java不允许您覆盖私有方法? 好吧,基本上,如果你能做到那么就没有办法写一个带有子类无法破解的抽象边界的类。 这将(IMO)成为一个主要的语言设计短缺。

为什么Java报告错误或警告? 那么没有错误,因为根据JLS,这是合法的Java。 至于警告......如果你单独编译Parent就没有问题,因为所写的代码是声明并使用私有方法。 如果单独编译Child ,则编译器无法在Parent类中看到private方法。 (实际上,它甚至可能不存在于您正在编译的Parent的.class文件版本中。)只有在您同时编译Parent和Child时,编译器才会发现有点奇怪的东西。

In the Parent class, you have declared printChild as private ... and called it. A private method cannot be overridden. Your printChild in the Child class is not known to the Parent class.

If you were to change the private modifier to public, then you would have an override, and your example should output what you are expecting.


Why won't Java let you override a private method? Well, basically, if you could do it then there would be no way to write a class with an abstraction boundary that a child class could not break. That would (IMO) be a major language design short-coming.

Why doesn't Java report an error or warning? Well there is no error because this is legal Java according to the JLS. As for a warning ... if you compile Parent in isolation there is no problem, because the code as written is declaring and using a private method. If you compile Child in isolation, the compiler can't see the private method in the Parent class. (Indeed, it may not even exist in the version of the .class file for Parent that you are compiling against.) Only if you compiled Parent and Child at the same time might the compiler spot something a bit odd.

更多推荐

本文发布于:2023-04-27 11:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   这段   代码   code   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!