Color:数组
public enum Color{ RED,BLUE,BLACK,YELLOW,GREEN }
Color字节码代码: :fa-exclamation-circle:枚举值都是public static final的,枚举值最好所有大写ide
final enum hr.test.Color { // 全部的枚举值都是类静态常量 public static final enum hr.test.Color RED; public static final enum hr.test.Color BLUE; public static final enum hr.test.Color BLACK; public static final enum hr.test.Color YELLOW; public static final enum hr.test.Color GREEN; private static final synthetic hr.test.Color[] ENUM$VALUES; }
//构造枚举值:RED(255, 0, 0) Color color=Color.RED;
1.ordinal()方法: 返回枚举值在枚举类种的顺序。这个顺序根据枚举值声明的顺序而定,0开始递增+1 2.compareTo()方法: 返回的是两个枚举值的顺序之差。(两个枚举值必须是同一枚举类,不然ClassCastException) 3.values()方法: 静态方法,返回一个包含所有枚举值的数组。 Color[] colors=Color.values();
Encodable:为code值变动提供接口this
Decoder:变换code值为枚举变量名code
FileType:枚举类对象
直接上代码:继承
FileType :接口
public enum FileType implements Encodable < String > { /** 文件 */ FILE( "0", "code.value.filetype.FILE" ), /** 文件夹 */ FOLDER( "1", "code.value.filetype.FOLDER" ), ; //values():表示获得所有的枚举内容,而后以对象数组的形式用foreach输出 private static final Decoder < String, FileType > decoder = Decoder.create( values() ); /** code值 */ //0 //1 private final String code; /** 属性文件的key值 */ //code.value.filetype.FILE //code.value.filetype.FOLDER private final String propertyKey; private FileType( String code, String propertyKey ) { this.code = code; this.propertyKey = propertyKey; } @Override public String encode() { return code; } @Override public String propertyKey() { return propertyKey; } /** * 将指定的code变换为对应的值{@link FileType}。 * * @param code code * @return FileType的值不存在的时候返回null。 */ public static FileType decode( final String code ) { FileType type = decoder.decode( code ); return type; } /** * 取得code对应的code名称 * * @param code code * @return code名称 */ public static String getName( final String code ) { FileType type = decode( code ); // ....可扩展 return type.propertyKey(); } }
Encodable:ci
public interface Encodable < T extends Serializable > { /** * 取得code值 * * @return code值 */ public T encode(); /** * 取得属性文件的key值 * * @return 属性文件的key值 */ public String propertyKey(); }
Decoder:get
public class Decoder < K extends Serializable, V extends Encodable < K >> { private final Map < K, V > map; /** * @param values 变换值list * @exception IllegalArgumentException code值重复 */ private Decoder( V[] values ) { map = new HashMap < K, V >( values.length ); for ( V val : values ) { V old = map.put( val.encode(), val ); // code值重复 if ( old != null ) { throw new IllegalArgumentException( "重复的code: " + val ); } } } /** * 变换code值 * * @param code code值 * @return 变换值 */ public V decode( K code ) { return map.get( code ); } /** * class做成 * * @param values 变换值list * @return Decoder Class */ public static < K1 extends Serializable, V1 extends Encodable < K1 >> Decoder < K1, V1 > create( V1[] values ) { return new Decoder < K1, V1 >( values ); } }
FileType.FILE----------------: FILEit
FileType.FILE.encode()-------: 0
FileType.decode( "0" )-------: FILE
FileType.FILE.propertyKey()--: code.value.filetype.FILE