class ParentClass
{
double height;
public ParentClass()
{
height=10;
System.out.println("this : "+this.getClass().getName());
showHeight();
}
private void showHeight()
{
System.out.println("Height is : "+height);
}
}
class ChildClass extends ParentClass
{
double weight;
public ChildClass(double weight)
{
this.weight=weight;
}
}
public class CallingPrivateMethod
{
public static void main(String arg[])
{
ChildClass childObj=new ChildClass(32.65);
}
}
这将打印以下内容:
这个:子类
高度为:10.0
最佳答案
您没有从子类调用私有(private)方法;它由父类(super class)(ParentClass)在其构造函数中调用,而该构造函数又由子类调用。这与基类调用私有(private)方法的任何其他时间没有什么不同。
关于java - 为什么可以在父类(super class)构造函数中调用子类对象的私有(private)方法......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34456910/