1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

enum 定義

Last updated at Posted at 2020-12-20

なんでENUMを使いますか。

→コーディングが分かりやすくなる

コード例)

①ENUMの定義

import java.util.HashMap;
import java.util.Map;

public enum FLAG {
    YES("1", "YES", "はい"),
    NO("2", "NO", "いいえ");

    public String v;
    public String nE;
    public String nJ;

    private FLAG(String v, String nE, String nJ) {
        this.v = v; this.nE = nE; this.nJ = nJ;
    }

    private static Map<String, FLAG> vMap = new HashMap<String, FLAG>();

    static {
        for(FLAG e : FLAG.values()) {
            vMap.put(e.v, e);
        }
    }

    public static FLAG vToE(String value) {
        return vMap.get(value);
    }

    public String getV() { return this.v; }
    public String getNE() { return this.nE; }
    public String getNJ() { return this.nJ; }
}

②機能にはENUMの値を呼び方法


    FLAG.YES.getV()   // 結果:"1"
    FLAG.NO.getV()    // 結果:"2"

    FLAG.YES.getNJ()  // 結果:"はい"
    FLAG.NO.getNJ()   // 結果:"いいえ"

③ENUMの比較
比較の時、文字列の比較の代わりに、ENUMを比較したの方が分かりやすい

    // ENUMに変換する
    ENUM YES_FLAG = FLAG.vToE("1"); // "1" : はい
    ENUM NO_FLAG  = FLAG.vToE("2"); // "2" : いいえ
    // ENUMの比較
    if(YES_FLAG == FLAG.YES) {
        // todo
    }

    if(NO_FLAG == FLAG.NO) {
        // todo
    }
    // JSPの場合、値の比較
    <c:if test="${FLAG.YES.getV() == '1'}">
    <c:if>

    // または
    <c:if test="${FLAG.NO.getV() == '2'}">
    <c:if>

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?