您的位置:首页 > 编程语言 > Java开发

枚举

2016-05-31 00:00 525 查看

典型枚举类型的定义

Color:

public enum Color{
RED,BLUE,BLACK,YELLOW,GREEN
}

Color字节码代码:
:fa-exclamation-circle:枚举值都是public static final的,枚举值最好全部大写

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;
}

特征及用法

枚举类就是class类,而且是一个不可以被继承的final类

即然枚举类是class,当然在枚举类型中有构造器,方法和数据域。
但是,枚举类的构造器有很大的不同:

构造器只是在构造枚举值的时候被调用

//构造枚举值:RED(255, 0, 0)
Color color=Color.RED;


构造器只能私有private
枚举类的方法和数据域可以允许外部访问

所有枚举类都继承了Enum的方法

1.ordinal()方法:
返回枚举值在枚举类种的顺序。这个顺序根据枚举值声明的顺序而定,0开始递增+1
2.compareTo()方法:
返回的是两个枚举值的顺序之差。(两个枚举值必须是同一枚举类,否则ClassCastException)
3.values()方法:
静态方法,返回一个包含全部枚举值的数组。
Color[] colors=Color.values();


枚举类可以在switch语句中使用

一种很好的枚举示例

Encodable:为code值变更提供接口

Decoder:变换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:

public interface Encodable < T extends Serializable > {

/**
* 取得code值
*
* @return code值
*/
public T encode();

/**
* 取得属性文件的key值
*
* @return 属性文件的key值
*/
public String propertyKey();
}

Decoder:

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----------------: FILE

FileType.FILE.encode()-------: 0

FileType.decode( "0" )-------: FILE

FileType.FILE.propertyKey()--: code.value.filetype.FILE
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 枚举 示例