package polymorphism.music;
import static net.mindview.util.Print.*;
class Stringed extends Instrument {
public void play(Note n) {
print("Stringed.play() " + n);
}
}
class Brass extends Instrument {
public void play(Note n) {
print("Brass.play() " + n);
}
}
public class Music2 {
public static void tune(Wind i) {
i.play(Note.MIDDLE_C);
}
public static void tune(Stringed i) {
i.play(Note.MIDDLE_C);
}
public static void tune(Brass i) {
i.play(Note.MIDDLE_C);
}
public static void main(String[] args) {
Wind flute = new Wind();
Stringed violin = new Stringed();
Brass frenchHorn = new Brass();
tune(flute); // No upcasting
tune(violin);
tune(frenchHorn);
}
} /* Output:
Wind.play() MIDDLE_C
Stringed.play() MIDDLE_C
Brass.play() MIDDLE_C
*///:~
This works, but there’s a major drawback: you must write type-specific methods for each new
Instrument class you add. This means more programming in the first place, but it also
means that if you want to add a new method like tune( ) or a new type of Instrument,
you’ve got a lot of work to do. Add the fact that the compiler won’t give you any error
messages if you forget to overload one of your methods and the whole process of working
with types becomes unmanageable
问:最后一句“Add the fact that the compiler won’t give you any error
messages if you forget to overload one of your methods and the whole process of working
with types becomes unmanageable”
是什么意思哈,我不太明白这句什么意思,是怎么个“overload”,这是《Thinking in java 》中的一段。
import static net.mindview.util.Print.*;
class Stringed extends Instrument {
public void play(Note n) {
print("Stringed.play() " + n);
}
}
class Brass extends Instrument {
public void play(Note n) {
print("Brass.play() " + n);
}
}
public class Music2 {
public static void tune(Wind i) {
i.play(Note.MIDDLE_C);
}
public static void tune(Stringed i) {
i.play(Note.MIDDLE_C);
}
public static void tune(Brass i) {
i.play(Note.MIDDLE_C);
}
public static void main(String[] args) {
Wind flute = new Wind();
Stringed violin = new Stringed();
Brass frenchHorn = new Brass();
tune(flute); // No upcasting
tune(violin);
tune(frenchHorn);
}
} /* Output:
Wind.play() MIDDLE_C
Stringed.play() MIDDLE_C
Brass.play() MIDDLE_C
*///:~
This works, but there’s a major drawback: you must write type-specific methods for each new
Instrument class you add. This means more programming in the first place, but it also
means that if you want to add a new method like tune( ) or a new type of Instrument,
you’ve got a lot of work to do. Add the fact that the compiler won’t give you any error
messages if you forget to overload one of your methods and the whole process of working
with types becomes unmanageable
问:最后一句“Add the fact that the compiler won’t give you any error
messages if you forget to overload one of your methods and the whole process of working
with types becomes unmanageable”
是什么意思哈,我不太明白这句什么意思,是怎么个“overload”,这是《Thinking in java 》中的一段。
解决方案 »
- linux下面有没有类似windowBuilder的eclipse中的GUI插件
- 请问 JAVA 检查代码的问题时,怎么快速的找到什么地方出问题了?
- 怎样对XML文件中的某个东西进行加密?
- 怎么把一个文本组件的Document对象清空掉
- 到底哪里出了问题?????
- 求教:用java可以写出实时的程序吗
- 什么是构造函数?他有什么用?
- java 数据库操作DBCP包问题
- 怎样显示到任务栏里?
- 谁有metrowerks公司的CodeWarrior Wireless Development Kit的注册码?
- statement.executeUpdate()执行返回0,sql语句放在plsql里面是正确的
- return 语句应该返回什么,这个函数结果该怎么表示
意思是说:
如果你忘记了重载你的方法,
编译器是不会报错的;
你的整个程序就不会按照预定的方式执行。