a********l 发帖数: 980 | 1 3. class Mammal {
4. String name = "furry ";
5. String makeNoise() { return "generic noise"; }
6. }
7. class Zebra extends Mammal {
8. String name = "stripes ";
9. String makeNoise() { return "bray"; }
10. }
11. public class ZooKeeper {
12. public static void main(String[] args) { new ZooKeeper().go(); }
13. void go() {
14. Mammal m = new Zebra();
15. System.out.println(m.name + m.makeNoise());
16. }
17. }
我是觉得要么输出是furry generic noise,要么是stripes bray
答案是furry bray,是为什么捏?明明就只有一个m啊?
谢谢! | s******e 发帖数: 493 | 2 like static methods, instance variables cannot be overriden, it can only be
hidden. | a********l 发帖数: 980 | 3 是说调用go()时class mammal中的makeNoise()不能overridden,只能hidden,所以就调
用了Class Zebra()的,变成"bray"了吗?
be
【在 s******e 的大作中提到】 : like static methods, instance variables cannot be overriden, it can only be : hidden.
| s******e 发帖数: 493 | 4 opposite. makeNoise is a method.
using which instance variable is decided at compilation time, in your case,
since the m is Mammal type, it will use instance variable from that class.
the method invokation will be decided at runtime based on the true entity of
your class (same as C++), so in your case, the true object type of m is
Zebra. | a********l 发帖数: 980 | 5 Got it, thanks!
,
of
【在 s******e 的大作中提到】 : opposite. makeNoise is a method. : using which instance variable is decided at compilation time, in your case, : since the m is Mammal type, it will use instance variable from that class. : the method invokation will be decided at runtime based on the true entity of : your class (same as C++), so in your case, the true object type of m is : Zebra.
| j*a 发帖数: 14423 | 6 typical polymorphism.
【在 a********l 的大作中提到】 : Got it, thanks! : : , : of
| q*********u 发帖数: 280 | 7 这个题彻底搞明白还是挺有意义的,前阵子我也发帖讨论过这个东西,
Mammal m = new Zebra();
内存里面,有两个name, makeNoise()说简单了是override, 说复杂了,是java中的方
法从来都是掩盖了cpp中virtual关键字,所以始终指向Zebra类里面定义方法。
3. class Mammal {
4. String name = "furry ";
5. String makeNoise() { return "generic noise"; }
6. }
7. class Zebra extends Mammal {
8. String name = "stripes ";
9. String makeNoise() { return "bray"; }
10. }
11. public class ZooKeeper {
12. public static void main(String[] args) { new ZooKeeper().go(); }
13. void go() {
14. Mammal m = new Zebra
【在 a********l 的大作中提到】 : 3. class Mammal { : 4. String name = "furry "; : 5. String makeNoise() { return "generic noise"; } : 6. } : 7. class Zebra extends Mammal { : 8. String name = "stripes "; : 9. String makeNoise() { return "bray"; } : 10. } : 11. public class ZooKeeper { : 12. public static void main(String[] args) { new ZooKeeper().go(); }
| g**e 发帖数: 6127 | 8 面试喜欢问这个
真正写代码没见过谁这么写
【在 q*********u 的大作中提到】 : 这个题彻底搞明白还是挺有意义的,前阵子我也发帖讨论过这个东西, : Mammal m = new Zebra(); : 内存里面,有两个name, makeNoise()说简单了是override, 说复杂了,是java中的方 : 法从来都是掩盖了cpp中virtual关键字,所以始终指向Zebra类里面定义方法。 : : 3. class Mammal { : 4. String name = "furry "; : 5. String makeNoise() { return "generic noise"; } : 6. } : 7. class Zebra extends Mammal {
| s***8 发帖数: 1136 | 9
,
of
good summary.
【在 s******e 的大作中提到】 : opposite. makeNoise is a method. : using which instance variable is decided at compilation time, in your case, : since the m is Mammal type, it will use instance variable from that class. : the method invokation will be decided at runtime based on the true entity of : your class (same as C++), so in your case, the true object type of m is : Zebra.
|
|