在超類的構造函數之外初始化的私有變量是否應該在子類中正確繼承?Java:超類中私有變量的初始化
例如,在下面的代碼中,私有實例變量x是否會在子類中正確繼承?我知道x和y只能在Rectangle中定義的2個公共getter才能在Square子類中訪問。但是,如果x在構造函數之外初始化,子類是否仍能成功繼承x?
public class Rectangle
{
private int x = 0;
private int y;
protected double height;
protected double length;
public Rectangle(double length, double height)
{
this.height = height;
this.length = length;
y = 0;
}
public int getx()
{
return x;
}
public int gety()
{
return y;
}
}
public class Square extends Rectangle
{
public Square(double side)
{
super(side, side);
}
}
是的。沒問題。 – Gendarme
私人成員根本沒有繼承。然而,它們按照程序文本初始化。 – EJP