Topic: 关于复写和重载 有些不明白 |
Print this page |
1.关于复写和重载 有些不明白 | Copy to clipboard |
Posted by: rainytooo Posted on: 2006-04-15 02:36 下面例子希望看看各位高手的解释 如果觉得例子不好也可以另外举个例子 如:车子 汽车什么的 class 声音 { public static final int 中音=0, 高音=1, 低音=2; } class 乐器{ public void 玩(int 声音){ System.out.println("乐器.玩()"); } } class 弹奏 extends 玩{ public void 玩(声音 n){ System.out.println("弹奏.玩(声音 n)"); } } public class 弹奏错{ public static void 旋律(乐器 i){ i.玩(声音.中音); } public static void main(String[] args){ 弹奏 长笛 = new 弹奏(); 旋律(长笛); } } 答案是什么? 我把写成中文了可能有不对的地方 原来是 class Notex { public static final int Middle=0, Sharp=1, Flat=2; } class InstrumentX{ public void play(int Notex){ System.out.println("Instrumentx.play()"); } } class Windx extends Instrumentx{ public void play(Notex n){ System.out.println("Windx.play(Notex n)"); } } public class WindError{ public static void tune(Instrumentx i){ i.play(Notex.Middle); } public static void main(String[] args){ Windx flute = new Windx(); tune(dlute); } } 写错了不要笑话我啊 有点拗口,好心人帮忙解释一下,万分感谢,我是自学JAVA,又是一个人 没人带,没人问,再加上我本来是金融系的,所以没环境,全靠看书,帮帮我啊! |
2.Re:关于复写和重载 有些不明白 [Re: rainytooo] | Copy to clipboard |
Posted by: moshen62 Posted on: 2006-04-16 15:40 显示的是: “弹奏.玩(声音 n)” 因为弹奏类把乐器类中的玩方法给重写了 而你又没有调用弹奏类中的父类的玩方法所以只显示 “弹奏.玩(声音 n)”这个字符串 |
3.Re:关于复写和重载 有些不明白 [Re: rainytooo] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2006-04-17 00:19 Q. What is the basic difference between the concept of method overloading and overriding? A: They are totally irrelevant except the name similarities in two senses. sense #1: English words overriding and overloading both have "over" inside sense #2:overloading methods will have the "same name" but different signature. overriding methods will have the "same name" and same signature. Except these above, there is nothing in common between them. Please read some more from the following QAs. Copied from http://bobcat.webappcabaret.net/javachina/faq/02.htm#Overload_Q1 There are a lot more discussion on this topic from the above link! |
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 |