「Java」Enum(列挙型)のメソッドの戻り値を利用するサンプル

2021年2月15日

書式
public enum 列挙型名
メンバーA(xx),
メンバーB(xx),
使用例
1.列挙EnumAaの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.tm;
public enum EnumAa {
AA(0),
BB(1),
CC(2);
private int value;
private EnumAa(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}
package com.arkgame.study.tm; public enum EnumAa { AA(0), BB(1), CC(2); private int value; private EnumAa(int value) { this.value = value; } public int getValue() { return this.value; } }
package com.arkgame.study.tm;

public enum EnumAa {

      AA(0),
      BB(1),
      CC(2);

      private int value;

      private EnumAa(int value) {
            this.value = value;
      }

      public int getValue() {
            return this.value;
      }

}

2.列挙EnumAaを利用
書式
列挙名.メンバー

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.tm;
public class EnumTest {
public static void main(String[] args) {
String strA = "test007";
String strB = null;
String strC = "study skill in arkgame.com";
System.out.println("文字列A(enum use): " + funcA(strA).getValue());
System.out.println("文字列B(enum use): " + funcA(strB).getValue());
System.out.println("文字列C(enum use): " + funcA(strC).getValue());
}
// enum型を返す
public static EnumAa funcA(String target) {
if (target == null || target.length() == 0) {
return EnumAa.AA;
} else if (target.length() > 10) {
return EnumAa.CC;
}
return EnumAa.BB;
}
}
package com.arkgame.study.tm; public class EnumTest { public static void main(String[] args) { String strA = "test007"; String strB = null; String strC = "study skill in arkgame.com"; System.out.println("文字列A(enum use): " + funcA(strA).getValue()); System.out.println("文字列B(enum use): " + funcA(strB).getValue()); System.out.println("文字列C(enum use): " + funcA(strC).getValue()); } // enum型を返す public static EnumAa funcA(String target) { if (target == null || target.length() == 0) { return EnumAa.AA; } else if (target.length() > 10) { return EnumAa.CC; } return EnumAa.BB; } }
package com.arkgame.study.tm;

public class EnumTest {

      public static void main(String[] args) {

            String strA = "test007";
            String strB = null;
            String strC = "study skill in arkgame.com";
            System.out.println("文字列A(enum use): " + funcA(strA).getValue());
            System.out.println("文字列B(enum use): " + funcA(strB).getValue());
            System.out.println("文字列C(enum use): " + funcA(strC).getValue());

      }

      // enum型を返す
      public static EnumAa funcA(String target) {
            if (target == null || target.length() == 0) {
                  return EnumAa.AA;
            } else if (target.length() > 10) {
                  return EnumAa.CC;
            }
            return EnumAa.BB;

      }

}

実行結果
文字列A(enum use): 1
文字列B(enum use): 0
文字列C(enum use): 2

Java

Posted by arkgame