LoginSignup
0
0

More than 5 years have passed since last update.

enum定義に数値を割り当てる方法

Posted at

C/C++/C#のenumのように定義に値を持たせたい場合の記述。
各定義のtoValue()を呼び出すことで数値の取得が行える。

Motionの列挙体定義
public enum Motion{
    Stop(0),
    Walk(1),
    Run(2),
    Jump(3),
    Count(4);

    // 実際の数値
    private int type;

    // コンストラクタ
    private Motion ( int type )
    {
        this.type = type;
    }

    // 数値の取得
    public int toValue ( )
    {
        return type;
    }
}
使用例
UserMotion[] userMotionArray = new UserMotion[Motion.Count.toValue()];
userMotionArray[Motion.Stop.toValue()] = new StopMotion();

Countは全体の定義の数を表してるので書いてあると便利かも?
確かenumObjectのスーパークラスにはlengthとかsizeメソッドは無かったはず・・・(確か・・・)

0
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
0
0