2012-05-15 34 views
0

考慮以下代碼:找不到符號 - 構造函數項()

// Create a Item oject 
item item = new item(); 

編譯器錯誤消息:

錯誤 - 無法找到符號 - 構造函數項();

item

public class item 
{ 
    // Attributes 
    private String Itemcode; 
    private String Itemname; 
    private String Description; 
    private String Style; 
    private String Finish; 
    private float Unitprice; 
    private float Stock; 
    private String Suppliercode; 
    private String Suppliername; 
    private String Address; 

    public item(String ItemcodeIn, String ItemnameIn, String DescriptionIn, 
       String StyleIn, String FinishIn, float UnitpriceIn, float StockIn, 
       String SuppliercodeIn, 
       String SuppliernameIn, String AddressIn) 
    { 
     Itemcode = ItemcodeIn; 
     Itemname = ItemnameIn; 
     Description = DescriptionIn; 
     Style = StyleIn; 
     Finish = FinishIn; 
     Unitprice = UnitpriceIn; 
     Stock = StockIn; 
     Suppliercode = SuppliercodeIn; 
     Suppliername = SuppliernameIn; 
     Address = AddressIn; 
    } 

而這個代碼隨後是所有的設置器/吸氣劑方法的屬性。

我該如何解決這個問題?

+1

注意:在Java中,習慣上使用'PascalCase'作爲類名,'camelCase'則使用變量名。所以,如果你遵循慣例,你的類名將是'Item',你的變量名將是'itemCodeIn','itemNameIn','item'等。 –

回答

10

當您創建參數化構造函數時,除非您創建自己的構造函數,否則將刪除默認構造函數。

因此,你必須明確地創建如下默認的構造函數:

public Item() {} 
6

您需要添加一個默認的構造函數:

public item() { 
    // any initialization you need here 
} 

你也應該將類重命名爲Item(資本I)根據最佳做法,您的字段應以小寫字母開頭。

2

Item類僅有1層構造:

public item(String ItemcodeIn, String ItemnameIn, String DescriptionIn, 
    String StyleIn, String FinishIn, float UnitpriceIn, float StockIn, 
    String SuppliercodeIn, String SuppliernameIn, String AddressIn) 

雖然你試圖通過new Item();訪問它,此構造不存在的,因爲你有一個參數化的一個過度纏身。

創建Item時,您應該提供這些參數或創建另一個,通用構造:

public Item() { 
    // Some code goes here 
} 
0

每當你不寫任何構造函數,默認的構造函數是由類中提供的默認。但是,如果您定義在任何類中寫入參數化構造函數,則只有該參數化構造函數可用。在這種情況下,你將不得不顯式地定義一個默認的構造函數。