簡體   English   中英

構造函數類JAVA的枚舉值

[英]enum value to constructor class JAVA

嗨,我正在創建數據庫表的bean,但是有一個小問題,我無法將ENUM值傳遞給構造函數,我解決了它,但是當我嘗試設置該值時,它表示錯誤; 這是我的代碼

public class HorariosBean {
    private Integer idHorario;
    private String dia;
    private Integer hora;
    private enum estado {DISPONIBLE, NODISPONIBLE};
    private Integer depasId;

    // this is the first thing I fix for pass the enum
    public HorariosBean(Integer idHorario, String dia, Integer hora, estado estate, Integer depasId) {
        this.idHorario = idHorario;
        this.dia = dia;
        this.hora= hora;
        this.estado = estate; //error is here says "estado cannot be resolved or is not a field"
        this.depasId = depasId;
    }
}

我無法更改類型,因為我有其他帶有枚舉的表,這意味着我應該更改所有內容

private enum estado {DISPONIBLE, NODISPONIBLE}; 表示您聲明了一個enum ,而不是變量。

因此,添加新行:

private estado yourEnum;

並更新您的構造函數:

this.yourEnum = estate;

另一個答案是正確的,但是還有另一個問題。

您不能將那個枚舉聲明為private 您會看到,您想要將該枚舉的實例傳遞給該類的構造函數。

但是其他類將無法使用該枚舉,例如在新的HorariosBean語句中,因為枚舉是私有的。

超越:不要欺騙自己,認為經常犯錯誤是不解決該問題的一個很好的理由。 換句話說:如果您反復犯同樣的錯誤,則必須觸摸所有代碼。 因此,避免代碼重復非常重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM