当前位置: 代码迷 >> J2SE >> 向上转型的有关问题
  详细解决方案

向上转型的有关问题

热度:95   发布时间:2016-04-24 02:10:17.0
向上转型的问题
Java code
package polymorphism.music;public enum Note {    MIDDLE_C, C_SHARP, B_FLAT; // Etc.}package polymorphism.music;class Instrument {  public void play(Note n) {    System.out.println("Instrument.play()");  }}package polymorphism.music;// Wind objects are instruments// because they have the same interface:public 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  }}

结果:

D:\my java>javac -d . Instrument.java Music.java Wind.java Note.java

D:\my java>java Music
Exception in thread "main" java.lang.NoClassDefFoundError: Music
Caused by: java.lang.ClassNotFoundException: Music
  at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Music. Program will exit.

我不明白为什么编译通过 但运行不了呢 那点出错了呢



------解决方案--------------------
CLASS文件没有找到Music
你看一下MUSIC.class文件在不在
------解决方案--------------------
路径不对......
如下:
D:\my java\polymorphism\music>javac -d . Instrument.java Music.java Wind.java No
te.java
D:\my java\polymorphism\music>java polymorphism.music.Music
  相关解决方案