1
public class HelloWorld
{
protected int num = 12;
public void callme()
{
System.out.print(this.num);
}
public static void main(String[] args)
{
HelloWorld myObject1 = new HelloWorld();
myObject1.callme();
OtherClass myObject2 = new OtherClass();
myObject2.callme();
}
}
public class OtherClass extends HelloWorld
{
protected int num = 14;
}
出力が「1214」ではなく「1212」になるのはなぜですか? PHPでは "1214"ですが、javaではその逆ではありません。その背後にある論理は何ですか?子クラスのJavaで同じ名前のプロパティ
これはPHPと何が関係がありますか?なぜPHPタグですか? –
可能な複製https://stackoverflow.com/questions/12589274/slight-confusion-regarding-overriding-where-variables-are-concerned – vinS
@Schwesiこれは、参照渡しとは関係がありません。値。これはフィールドの隠蔽です。 –