Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:请教:怎样理解“函数的重载”? [Re:WWWLS]
bluecrystal





发贴: 2788991
积分: 48
于 2005-10-02 22:31 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
gongshi给的链接讲解的非常清楚
我刚刚看了一遍,其中这段qa推荐大家能好好的看看,并且能够深入的理解一下
因为下面这段除了涉及到overload以及override之外,在多态性方面也是一个极佳的例子

Q. Can you explain the result of the following example? Oh, my!

class Base {
public boolean foo(Base b) {
return true;
}
}
class Sub extends Base {
public boolean foo(Sub s) {
return false;
}
}

public class Test {
public static void main(String argv[]) {
Base bb = new Base();
Base bs = new Sub();
Sub ss = new Sub();

System.out.println(bb.foo(bb)); //true

System.out.println(bs.foo(bs)); //true ???
System.out.println(bs.foo(ss)); //true ???
System.out.println(bs.foo(bb)); //true ???
System.out.println(ss.foo(bs)); //true ???
System.out.println(ss.foo(bb)); //true ???

System.out.println(ss.foo(ss)); //false
}
}
A: The foo methods are overloaded in the Sub. In Base, there is only one foo method, and it is not overridden by the Sub!
Overloading is fundamentally different then overriding. There is no polymorphism or dynamic binding here!!!
All decisions are made at compile time!!! See detailed explanation in the same code below, documented!

class Base {
// There is only one foo method in Base!!!
public boolean foo(Base b) {
return true;
}
}
class Sub extends Base {
// differnt signature, method overloading
// there are 2 foo methods in the Sub
public boolean foo(Sub s) {
return false;
}
}

public class Test {
public static void main(String argv[]) {
// bb is a Base ref to the compiler, Base obj at runtime
Base bb = new Base();
// bs is a Base ref to the compiler, Sub obj at runtime
Base bs = new Sub();
// ss is a Sub ref to the compiler, Sub obj at runtime
Sub ss = new Sub();

// All these 4 lines are Base ref
// call Base.foo(Base) method, and only one foo available
// bs is a Base ref, and ss ISA Base ref too
// Everything is fine!!!
System.out.println(bb.foo(bb)); //true
System.out.println(bs.foo(bs)); //true
System.out.println(bs.foo(ss)); //true
System.out.println(bs.foo(bb)); //true

// bb, bs are both Base refs to Compiler
// ss is a Sub ref
// call Sub.foo(Base) which is inherited from Base
System.out.println(ss.foo(bs)); //true
System.out.println(ss.foo(bb)); //true

// no doubt about this one, am I right?
System.out.println(ss.foo(ss)); //false
}
}



bluecrystal edited on 2005-10-02 22:34

Just Software & Travel
-- 我的blog -- 技术点滴/经验分享

话题树型展开
人气 标题 作者 字数 发贴时间
9755 请教:怎样理解“函数的重载”? WWWLS 996 2005-10-02 18:15
7493 Re:请教:怎样理解“函数的重载”? bluecrystal 1376 2005-10-02 20:50
8048 Re:请教:怎样理解“函数的重载”? ranchgirl 794 2005-10-02 21:52
7445 Re:请教:怎样理解“函数的重载”? ranchgirl 134 2005-10-02 22:03
8414 Re:请教:怎样理解“函数的重载”? bluecrystal 2554 2005-10-02 22:31

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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