2016-02-26 86 views
0

接收到大多數結果爲Define the constructor, homeboy的此錯誤。深入瞭解我所犯的錯誤,因爲我認爲它是在我的課堂上定義的。我對Java很新,如果它很明顯,不要撕碎我。Java構造函數錯誤:已定義構造函數,未找到

Error: constructor KServer in class KServer cannot be applied to given types; 
       KServer server = new KServer(port); 
    required: no arguments 
    found: int 
    reason: actual and formal argument lists differ in length 
1 error 

KServer.java

public class KServer { 
    private int port; 

    //isn't this the constructor defined? 
    public void KServer(int PORT) { 
     port = PORT; 
    } 
    public void Run() {...} 
} 

KServ.java

public class KServ { 
    public static void main(String[] args) { 

    if (args.length != 1) { 
     System.err.println("Usage: java KServ <port number>"); 
     System.exit(1); 
    } 

    int port = Integer.parseInt(args[0]); 
    KServer server = new KServer(port); 
    server.Run(); 

    } 

} 
+3

從您的「構造函數」 – Reimeus

+0

中刪除'void'關鍵字,它必須是noob錯誤。拉屎。它在技術上不是無效的嗎? – Chemistpp

+1

一個構造函數沒有返回類型 - 所以我想你的「構造函數」被解釋爲方法 – Rhayene

回答

2

沒有返回類型的構造函數,否則你定義一個方法。

public KServer(int PORT) { 
    port = PORT; 
} 
+1

你贏了幾個秒。該死的 ! Upvote爲您的勝利。 –

3

從構造函數定義中刪除字無效:

public KServer(int PORT) { 
    port = PORT; 
} 

有關如何編寫構造函數,你可以看看here更多細節。

+1

JP贏得了比賽,我會接受他的回答,但你仍然會得到一個冷靜的upvote;) – Chemistpp

+0

@Chemistpp哈哈。是啊。 –