参考书籍:thinking in java第四版 P149 一个简单的多态
package polymorphism.music;
package polymorphism.music; enum Note { MIDDLE_C, C_SHARP, B_FLAT; // Etc. } ///:
package polymorphism.music; import static net.mindview.util.Print.*; class Instrument { public void play(Note n) { print("Instrument.play()"); } }
package polymorphism.music; // Wind objects are instruments // because they have the same interface: class Wind extends Instrument { // Redefine interface method: public void play(Note n) { System.out.println("Wind.play() " + n); } } ///:~
package polymorphism.music; public class Music { public static void tune(Instrument i) { // ... i.play(Note.MIDDLE_C); } public static void main(String[] args) { Wind flute = new Wind(); tune(flute); // Upcasting } } /* Output: Wind.play() MIDDLE_C *///:~
就是一个简单的多态,其中这几个类文件在同一个目录下。不过我在command line下编译时 编译通过 却提示无法找到或加载类文件
我在Eclipse下,顺利地跑起来了。
求解,这是为何?
你看下是不是有包没有引入 ,如果有包的话要在执行的类名前加包名和\
再次自问自答吧。
http://stackoverflow.com/questions/18093928/what-does-could-not-find-o...
看这个就行了
谢谢 @xelz
你只用javac编译了Music类,没有编译其他的类
你应该在D:\Code\TIJ4-code>
下执行
shelljavac polymorphism/music/*.java
java polymorphism.music.Music