Topic: 请教一个JAVA初级的问题 |
Print this page |
1.请教一个JAVA初级的问题 | Copy to clipboard |
Posted by: nick_z Posted on: 2004-03-17 23:39 public class Test { public static void main(String[] args) { Derived d1 = new Derived(); d1.add(d1); //这个地方应该调的是哪一个? } } class Base { public void add(Base base) { System.out.println("now in Base"); } public void add(Derived base) { System.out.println("now in Base2"); } } class Derived extends Base { public void add(Base base) { System.out.println("now in derived1"); } } 编译器给的答案是: "Test.java": reference to add is ambiguous; both method add(Derived) in Base and method add(Base) in Derived match at line 8, column 12 为什么不调用Base.add(Derived base)呢?参数类型完全匹配啊? 原因何在呢? 望大侠帮忙解释一下,谢谢! |
2.Re:请教一个JAVA初级的问题 [Re: nick_z] | Copy to clipboard |
Posted by: lwmc Posted on: 2004-03-18 10:07 回复的是now in Base2啊! |
3.Re:请教一个JAVA初级的问题 [Re: nick_z] | Copy to clipboard |
Posted by: fuyistone Posted on: 2004-03-18 20:05 首先,将你的代码在Jcreator中编译没有错误! 若将你的三个打印函数按顺序编号为1,2,3 那么编译器选择的顺序为2,3,1 即按照先在继承类中找完全匹配的,再在基类中找的顺序。 至于参数的选择,同样先找Derived类型,若无精确匹配,退而求其父,找base类型的。 |
4.Re:请教一个JAVA初级的问题 [Re: nick_z] | Copy to clipboard |
Posted by: nick_z Posted on: 2004-03-18 20:39 奇怪,我用JBuilder 9 编译就会出现这样的提示: "Test.java": reference to add is ambiguous; both method add(Derived) in Base and method add(Base) in Derived match at line 6, column 4 但是我用JDK (和JBuilder用的是同一个JDK)的命令行 javac Test.java 编译就能正常通过. 是JBuilder的问题吗???? |
5.Re:请教一个JAVA初级的问题 [Re: nick_z] | Copy to clipboard |
Posted by: mummy_zc Posted on: 2004-03-25 09:00 Derived is a a Based. so when u define add method ,it's not an overload feature.because the arguments is the same kind of things,it's the same with int and char make no difference to the overload feature. { public void add(Base base) { System.out.println("now in Base"); } public void add(Derived base) { System.out.println("now in Base2"); } } |
6.Re:请教一个JAVA初级的问题 [Re: nick_z] | Copy to clipboard |
Posted by: mummy_zc Posted on: 2004-03-25 09:09 sorry ,i don't take a serious consideration about this question.so i make a mend about that. the overload works all right,because even though Drived is a Based,Based is not a Drived.so it's different to the compiler. d1.add(d1); //it's illegal here,because Derived is a Based,and Derived is also a Derived,so it's ambigious d1.add;//it's ok because Base is not a Drived. |
7.Re:请教一个JAVA初级的问题 [Re: nick_z] | Copy to clipboard |
Posted by: kjjx Posted on: 2004-04-05 23:44 多态性,在你的基类里add有两个版本 public void add(Base base) public void add(Derived base) 当你调用Derived.add(Derived)时首先查找Derived里由没有重载版本,如果没有 就上朔到基类查找,找到就调用之,若没有就查找子类里其他重载版本,一般编译程序都在各个重载类里维持一个虚拟函数表确定其对应关系(深入前出MFC e/2说得比较明白),调用顺序依次是 derived.add(derived) base.add(derived) derived.add(base) base.add(base);当父类与子类有同类型函数时子类版本总被优先调用. |
8.Re:请教一个JAVA初级的问题 [Re: nick_z] | Copy to clipboard |
Posted by: pengtaoli Posted on: 2004-04-20 19:59 可以看看继承中的函数覆盖(override) |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |