Javaで列挙型を使用する最も一般的な正しい方法は何ですか? クラス コンストラクターやファクトリ メソッド パラメーターなどで列挙型を使用する必要がある場合、最初に考えられるのはenum
、クラス内に public static を含めてインポートすることです。
package mypackage;
import mypackage.Data.ContentType;
class Data {
public static enum ContentType { TYPE1, TYPE2, TYPE3 }
ContentType type;
String value;
public Data( String value, ContentType type ) {
this.value = value;
this.type = type;
}
}
使用例: new Data( "some value", ContentType.TYPE1 );
.
人々が下のような構造を好む理由は何ですか?
public static final int TYPE1 = 1;
public static final int TYPE2 = 2;
public static final int TYPE3 = 3;