2017-06-05 74 views
-1
public Tipo getTipo() { 
    return this.Importo < 0.0 ? Tipo.USCITA : Tipo.ENTRATA; 
} 

public int compareTo(Movimento m) { 
    if (this.idConto != this.idConto) { 
     return this.idConto - this.idConto; 
    } 
    return this.DataMov.compareTo(this.DataMov); 
} 

public static enum Tipo { 
    ENTRATA,// here i have this error : The constructor Movimento.Tipo() is undefined 
    USCITA;// here is the same : The constructor Movimento.Tipo() is undefined 


    private Tipo(String string2, int n2) { 
    } 
} 

我已經有構造函數,我需要,還有什麼我需要寫?構造函數未定義?枚舉錯誤

+2

枚舉你寫一個構造函數有兩個參數,但沒有默認的構造函數。這意味着編譯器不會提供無參數構造函數。你應該提供一個。 – duffymo

回答

1

我不知道你想如何定義枚舉。基本上有2個解決這個:

1.定義沒有參數枚舉

public static enum Tipo { 
    ENTRATA, 
    USCITA; 
} 

2.定義與參數

public static enum Tipo { 
    ENTRATA("Entrata", 1), 
    USCITA("Uscita", 2); 

    private String string; 
    private int integer; 

    private Tipo(String string, int integer) { 
     this.string = string; 
     this.integer = integer; 
    } 
} 
+0

它的工作原理!非常感謝 –

0

你寫錯了枚舉。

public enum abc { 

    ENTRATA("abc", 1),// here i have this error : The constructor Movimento.Tipo() is undefined 
    USCITA("xyz", 2);// here is the same : The constructor Movimento.Tipo() is undefined 


    private abc(String string2, int n2) { 
    } 

}

+0

如果有枚舉2參數的構造然後枚舉變量包含兩個參數,因爲在枚舉每個變量作爲一個對象。 –

1

你寫一個構造函數有兩個參數,但沒有默認構造函數。這意味着編譯器不會提供無參數構造函數。您應該提供一個或刪除私有構造函數。

我認爲沒有理由有兩個參數的私有構造。您的枚舉中沒有任何私有數據成員。

爲什麼你的枚舉靜態的?刪除。

public enum Tipo { 
    ENTRATA, USCITA; 

} 
+0

由於OP沒有編譯器,所以編寫一個默認構造函數對於OP來說是一個相當大的挑戰。 – Tom

+0

我認爲這是他的代碼。我發佈的代碼將有一個無參數構造函數。我不明白這個評論。 – duffymo

+0

你寫*「你寫一個構造函數有兩個參數,但沒有默認構造函數。這意味着,編譯器不會提供一個無參數的構造函數。」 *,但它周圍的其他方法。構造函數不會創建**默認的**構造函數,因爲OP定義了他自己的構造函數。所以OP需要創建** no-arg **構造函數。 – Tom