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啊?
谢谢! |
|
q*********u 发帖数: 280 | 2 这个题彻底搞明白还是挺有意义的,前阵子我也发帖讨论过这个东西,
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 |
|
J**B 发帖数: 204 | 3 Given:
3. interface Animal { void makeNoise(); }
4. class Horse implements Animal {
5. Long weight = 1200L;
6. public void makeNoise() { System.out.println("whinny"); }
7. }
8. public class Icelandic extends Horse
{ 9. public void makeNoise() { System.out.println("vinny"); }
10. public static void main(String[] args) {
11. Icelandic i1 = new Icelandic();
12. Icelandic i2 = new Icelandic(); 12. Icelandic i3 = new Icelandic();
13. i3 = i1; i1 = i2; i2 = null; i3 = i1;
14. } 15. }
When line 14 is r... 阅读全帖 |
|
b******d 发帖数: 794 | 4 请问这段代码为什么结果是"furry bray stripes", 同一个object, 用不同reference
type引用, object里面的variable的值就会变吗,还是本身object里面就有好几个同名
vairable分属于inheritence tree里面不同classes?
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() {
... 阅读全帖 |
|
m*****k 发帖数: 731 | 5 I think so,
you can simply try this,
class Mammal {
String name = "furry ";
protected String protected_name = "protected ";
private String private_name = "private ";
String makeNoise() {
return "generic noise";
}
}
class Zebra extends Mammal {
String name = "stripes ";
String makeNoise() {
return "bray ";
}
}
public class ZooKeeper {
public static void main(String[] args) {
new ZooKeeper().go();
}
void go() {
Zebra z = n... 阅读全帖 |
|
a********l 发帖数: 980 | 6 是说调用go()时class mammal中的makeNoise()不能overridden,只能hidden,所以就调
用了Class Zebra()的,变成"bray"了吗?
be |
|
s******e 发帖数: 493 | 7 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. |
|
p*********t 发帖数: 2690 | 8 3. class Mammal {
4. String name = "furry ";
5. String makeNoise() { return "generic noise"; }
6. }
里面的name不是public variable,而是package member.
subclass can access to all the public,package and protected members of the
superclass.
找本教科书吧。
开辟
private |
|
|