class Parent {
public Parent() {
System.out.println("Parent Default..");
System.out.println("Object type : " + this.getClass().getName());
this.method();
}
private void method() {
System.out.println("private method");
}
}
class Child extends Parent {
public Child() {
System.out.println("Child Default..");
}
public static void main(String[] args) {
new Child();
}
}
调用私有方法。当我运行这段代码将打印“这个” =儿童 的类名,但“此”对象是能够调用为什么父类的私有方法?从子对象
您需要在父类中删除'this',因为当您在此时调用继承时,它指向子类引用,因此它不会调用该继承。 –
,因为这是父母的私有方法,如果这=父母,那么它称之为自己的方法 –
@DanialHussain请首先运行代码,然后做出决定。这可以调用私有方法。 –