2013-10-09 24 views
2

我知道這個問題已經被問過很多次了,但我並沒有真正理解爲什麼我面臨這個問題。在博客和帖子提供的答案,在我的代碼已經實施,我仍面臨這個問題(或我仍然無法弄清楚,爲什麼我的代碼編譯失敗)構造函數 - 不能應用於給定的類型 - 必需:無參數

public class Utilities { 
    public Client client = null; 
    private static object x = null; 

    public Utilities(Client client) throws Exception { 
     this.client = client; 
     //CODE GOES HERE 
    } 
} 

我調用這個類在其他文件作爲,Utilities utile = new Utilities(client); 當我編譯這個代碼,我得到以下錯誤,

constructor Utilities in class Utilities cannot be applied to given types 
required: no arguments 
found: Client 
reason: actual and formal argument lists differ in length 

通過幾個論壇帖子和博客的打算後,我增加了默認的承包商,現在我的代碼看起來像,

public class Utilities { 
    public Client client = null; 
    private static object x = null; 

    private Utilities() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    public Utilities(Client client) throws Exception { 
     this.client = client; 
     //CODE GOES HERE 
    } 
} 

但仍然是一樣的錯誤。任何線索我在這裏做錯了什麼。

+13

我強烈懷疑你有兩個類稱爲'Utilities',和你的「等」是一個構造函數調用* *是指。 –

+0

@JonSkeet這也是我的預感。這裏的錯誤是說你的'Utilities'類不能接受'Client'。 – Cruncher

+0

Utilities utile = new Utilities(client);什麼是「客戶」? – Andy897

回答

0

試試這個代碼,讓我知道:

public class Utilities { 
    private Client client; 
    private Object x; 

    //this is the normal structure of a constructor in java classes 
    public Utilities(Client client){ 
     this.client = client; 
     //CODE GOES HERE 
    } 
} 
相關問題