【发布时间】:2015-06-06 01:13:52
【问题描述】:
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();
【问题讨论】:
-
那是因为
Child没有printParent方法 -
是的,但它确实有 printChild 方法,所以当我调用 this.printChild() 它应该使用 child 方法,不是吗?
-
私有方法不能被覆盖。 stackoverflow.com/questions/11976446/…
标签: java inheritance