参考书籍: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下,顺利地跑起来了。
求解,这是为何?
你应该在
D:\Code\TIJ4-code>
下执行