标题:Thinking in Java中上溯造型的疑问
只看楼主
Sieben7
Rank: 2
等 级:论坛游民
帖 子:116
专家分:20
注 册:2007-4-16
 问题点数:0 回复次数:2 
Thinking in Java中上溯造型的疑问
import java.util.*;

class Instrument3 {
public void play() {
System.out.println("Instrument3.play()");
}
public String what() {
return "Instrument3";
}
public void adjust() {}
}

class Wind3 extends Instrument3 {
public void play() {
System.out.println("Wind3.play()");
}
public String what() { return "Wind3"; }
public void adjust() {}
}

class Percussion3 extends Instrument3 {
public void play() {
System.out.println("Percussion3.play()");
}
public String what() { return "Percussion3"; }
public void adjust() {}
}

class Stringed3 extends Instrument3 {
public void play() {
System.out.println("Stringed3.play()");
}
public String what() { return "Stringed3"; }
public void adjust() {}
}

class Brass3 extends Wind3 {
public void play() {
System.out.println("Brass3.play()");
}
public void adjust() {
System.out.println("Brass3.adjust()");
}
}

class Woodwind3 extends Wind3 {
public void play() {
System.out.println("Woodwind3.play()");
}
public String what() { return "Woodwind3"; }
}

public class Music3 {
// Doesn't care about type, so new types
// added to the system still work right:
static void tune(Instrument3 i) {
// ...
i.play();
}
static void tuneAll(Instrument3[] e) {
for(int i = 0; i < e.length; i++)
tune(e[i]);
}
public static void main(String[] args) {
Instrument3[] orchestra = new Instrument3[5];
int i = 0;
// Upcasting during addition to the array:
orchestra[i++] = new Wind3();
orchestra[i++] = new Percussion3();
orchestra[i++] = new Stringed3();
orchestra[i++] = new Brass3();
orchestra[i++] = new Woodwind3();
tuneAll(orchestra);
}
}


搞不清what()和adjust()这两个方法要实现什么功能?即使把他们注释掉也不影响整个程序的运行。
点拨一下吧~o(∩_∩)o..谢谢~~








[此贴子已经被作者于2007-5-15 14:27:17编辑过]

搜索更多相关主题的帖子: Thinking Java color 疑问 
2007-05-15 14:26
支离破碎
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:737
专家分:0
注 册:2007-1-4
得分:0 
溯造.....
不就是重写父类的方法吗?你都没调用这二个方法所以,去不去掉不影响的

人生漂泊無依,有如浮萍菱花,隨水飄流,你会在我这里停留吗?
[url=http://51mynet.]http://51mynet.[/url]
2007-05-15 14:37
Sieben7
Rank: 2
等 级:论坛游民
帖 子:116
专家分:20
注 册:2007-4-16
得分:0 
谢谢斑斑
2007-05-16 10:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-139561-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.498933 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved