您的位置:首页 > 其它

枚举类型Enum用来存放系统常量

2016-04-12 12:29 519 查看

enum存放常量实例代码

1、写接口,枚举类型的例子

public interface EnumValue<V> {

/**
* Returns this enum wrapper object value.
*/
public V getValue();
}


改进版

public interface EnumValue<K,V> {

/**
* @return 返回这个枚举对象的值
*/
public K getValue();

/**
* @return 返回这个值的描述
*/
public V getMsg();
}


2、写Enum类型的

/**
* @since 2.0
*/
//这里说明一下,还可以加一些私有变量,放在构造函数里面
public enum ConstantsUtils implements EnumValue<String> {
PHP_SUCCESS(1, "返回成功"), PHP_ERROR(-1, "返回失败"),
CHECKPARA_SUC(1, "检验成功"), ;

final int code;
final String name;

public int getCode() {
return code;
}

public String getName() {
return name;
}

private ConstantsUtils(int code, String name) {
this.code = code;
this.name = name;
}

@Override
public String getValue() {
return code + "";
}
}


3、使用如下代码

public class TestEnum {

public static void main(String[] args) {
if (ConstantsUtils.PHP_SUCCESS.getCode()!=1) {
System.out.println("你好陌生人");
}elseif(ConstantsUtils.PHP_SUCCESS.getValue()==1){
System.out.println("显然这句话会被打印出来");
}
else{
System.out.println("显然不成立");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: