use*_*155 5 java inheritance casting
我有一个关于Java继承和转换的问题.我有以下两个示例类和一个测试类,我在类之后陈述我的问题:
public class Automobile {
public int var;
public Automobile () {
var = 1;
}
public String toString () {
return "AUTOMOBILE: " + var;
}
}
public class Porsche extends Automobile {
public int var;
public Porsche () {
var = 2;
}
public String toString () {
return "PORSCHE: " + var;
}
}
public class Test {
public static void main (String [] args) {
Porsche p = new Porsche();
Automobile a = new Automobile();
System.out.println ("(Automobile) p = " + (Automobile)p);
System.out.println ("(Automobile) p.var = " + ((Automobile)p).var);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
(Automobile) p = PORSCHE: 2
(Automobile) p.var = 1
Run Code Online (Sandbox Code Playgroud)
我不明白为什么在第二个声明中我们有1.它不应该是2吗?因为我在第一个声明中将p转换为汽车之后我仍然得到PORSCHE: 2了p- 我通过以下方式理解:
尽管如此,我仍然将p汽车p保留为"原始性质" - p是保时捷的一个对象,但由于保时捷延伸了汽车,我们可以说p也是汽车.因此,当我们将它明确地转移到汽车时,它会继续使用它的方法 - 在我们的例子toString()中是保时捷中定义的方法.
另一方面,如果我写的是正确的,那么第二个print语句应该给2而不是1.
现在这似乎与我相矛盾,但由于这在Java中起作用,似乎我不明白在转换和继承过程中发生了什么.
隐藏变量var.
你Porche.var的变量不同于Automobile.var.
该Porsche.toString()方法使用Porsche.var,而演员告诉编译器使用该Automobile版本.
| 归档时间: |
|
| 查看次数: |
6620 次 |
| 最近记录: |