2012-07-23 91 views
2

我剛開始我的Java教程和我的第一個工作的一部分,我被要求:新來Java和不能修復錯誤;類,接口或枚舉的預期。 274062

一)創建一個現實世界對象的新類(我選擇手機):移動 b)對於這個類創建一個接口來定義它的行爲,然後要求你的類實現它。

我用的Netbeans爲這項工作,這是我寫的代碼/擴展:

package mobile; 

    /** 
    * @param args the command line arguments 
    */ 

    public static void main(String[] args) { 

public class Mobile { 
    int volume = 0; 
    int ringtone = 0; 

void volumeUp (int increment){ 
    volume = volume + increment; 
} 
void changeringtone (int newValue){ 
    ringtone = newValue; 
} 
void volumeDown (int decrement){ 
    volume = volume - decrement; 
} 
void printStates(){ 
    System.out.println("ringtone:" + ringtone + "volume:" + volume); 
} 

}

然而,當我運行它,我得到這個錯誤信息:

java.lang.NoClassDefFoundError: mobile/Mobile (wrong name: mobile/mobile) 
at java.lang.ClassLoader.defineClass1(Native Method) 
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) 
at java.lang.ClassLoader.defineClass(ClassLoader.java:615) 
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) 
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) 
at java.net.URLClassLoader.access$000(URLClassLoader.java:58) 
at java.net.URLClassLoader$1.run(URLClassLoader.java:197) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 

找不到主要類:mobile.Mobile。程序將會退出。

我懇請任何人都可以請,首先,向我指出我的代碼中的錯誤,其次,我如何解決Netbeans中的這些問題。

預先感謝您!

karramelle

+0

在這個網站上它被認爲是良好的行爲來選擇你認爲最有用的答案,然後單擊選中標記接受:

簡要

所以它。如果你這樣做,我們將不勝感激。謝謝! – 2012-07-24 13:23:19

回答

0

在這種情況下,除非您正在編寫代碼來測試您的類,否則您可能甚至不需要main()方法。正如thinksteep建議的那樣,您可以在Mobile類中移動main()方法。或者,您可以將它們一起移除以編譯您的代碼。但是,請注意,爲了運行您的程序,您必須有一個main()方法

+0

嗨Code-Guru,感謝您的提示!當然我在學習java的過程中會考慮它。乾杯! – user1546822 2012-07-23 23:09:09

+0

我很高興能幫上忙。如果您有任何其他問題,請回來。 – 2012-07-25 19:01:09

4

主要方法(任何方法)應該在課堂上。包&進口後,應該是類。請花一些時間閱讀Java Syntax

public class Mobile { 
    //Variables declarations 
    //Instance blocks etc., 
    public static void main(String[] args) { 

//Your code here 
} 

} 
+0

嗨thinksteep,非常感謝。你是對的Java語法,這就是我花了最後1個半小時閱讀。我從網站獲得了一些見解並設法解決錯誤。 – user1546822 2012-07-23 23:01:07

2

除了java語言問題(方法駐留在類體內)之外,我想鼓勵您更改小應用程序的體系結構

系統會要求您先編寫接口,然後再編寫該接口的實現。你已經選擇了一款手機,不錯,但你的Mobile應該是接口,另一個類(IPh**eNo**a)應該提供該接口的真正實現。

然後我會使用第三個類的主要方法。將其命名爲User - 這將是在實現Mobile的實例上調用該方法的類。

public class Me { 
    public static void main(String[] args) { 
    Mobile myPhone = new Nokia(); // we "buy" a mobile 
    myPhone.setVolume(11);   // that's more then 10 ;) 
    } 
} 

public interface Mobile { 
    void setVolume(int level); // as an example 
} 

public class Nokia implements Mobile { 
    // (1) a constructor 
    // (2) implementations for all methods defined on Mobile 
} 
+1

嗨Andreas_D,非常感謝。我設法解決我的第一個錯誤,現在我試着回答練習中提出的問題的其餘部分,是的,您的回答在創建定義對象行爲的接口方面很有幫助,然後使類能夠實現它。再次感謝! – user1546822 2012-07-23 23:06:05

相關問題