由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - SCJP书上某习题一问
相关主题
java的一个问题2 Java Developer SCJD books download
考一个sun certified programmer 310-035 大约需要多久?apply Java board BF
初学java谁给推荐一本入门书Re: 刚过了SCJP, 灌水一篇,以示庆贺;兼问SCJD和SCEA
这个版太冷了static vs. final
c++熟手如何学习Java直到能够参与开发企业级应用?maybe useful SCJP-mock exam link list
请教个garbage collector问题PASS SCJP LE
SCJP Books两个很基本的JAVA问题
Re: Want to take Java CertificationSCEA 1.4 beta exam (Free)
相关话题的讨论汇总
话题: string话题: mammal话题: zebra话题: makenoise话题: class
进入Java版参与讨论
1 (共1页)
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.

1 (共1页)
进入Java版参与讨论
相关主题
SCEA 1.4 beta exam (Free)c++熟手如何学习Java直到能够参与开发企业级应用?
Anyone took Java Architect cert test?请教个garbage collector问题
大家能推荐考JAVA证书的中文网站吗?SCJP Books
问个知识升级的问题Re: Want to take Java Certification
java的一个问题2 Java Developer SCJD books download
考一个sun certified programmer 310-035 大约需要多久?apply Java board BF
初学java谁给推荐一本入门书Re: 刚过了SCJP, 灌水一篇,以示庆贺;兼问SCJD和SCEA
这个版太冷了static vs. final
相关话题的讨论汇总
话题: string话题: mammal话题: zebra话题: makenoise话题: class