java枚举类型enum的使⽤实例
java枚举类型enum的使⽤
java中定义常量有两种⽅法:final关键字和enum枚举。Java中的枚举类型采⽤关键字enum来定义,从jdk1.5才有的新类型,所有的枚举类型都是继承⾃Enum类型。要了解枚举类型,建议⼤家先打开jdk中的Enum类简单读⼀下,这个类⾥⾯定义了很多protected⽅法,⽐如构造函数,如果要使⽤这些⽅法我们可以把枚举类型定义到当前类中。每个枚举类型,都有⾃⼰的名字和顺序,当我们输出⼀个枚举类型的时候,会输⼊枚举类型的name,具体可以参考下⾯的例⼦。
public enum OperationType {
ADD_KEYWORD("add_keyword@resource", "KEYWORD"),
ADD_START_KEYWORD("add_start_keyword@resource", "KEYWORD"),
ADD_STOP_KEYWORD("add_stop_keyword@resource", "KEYWORD"),
DELETE_KEYWORD("delete_keyword@resource", "KEYWORD"),
ADD_REC_TO_RECYCLE("add_rec_to_recycle@resource", "KEYWORD"),
STOP_KEYWORD("stop_keyword@resource", "KEYWORD"),
STOP_ALL_KEYWORD("stop_all_keyword@resource", "KEYWORD"),
START_KEYWORD("start_keyword@resource", "KEYWORD"),
START_ALL_KEYWORD("start_all_keyword@resource", "KEYWORD"),
BID_KEYWORD("bid_keyword@resource", "KEYWORD"),
MODIFY_BID_KEYWORD("modify_bid_keyword@resource", "KEYWORD"),
MODIFY_ADD_KEYWORD("modify_add_keyword@resource", "KEYWORD"),
MODIFY_SUB_KEYWORD("modify_sub_keyword@resource", "KEYWORD"),
MODIFY_KEYWORD_TAG("modify_keyword_tag@resource", "KEYWORD"),
CANCEL_KEYWORD_TAG("cancel_keyword_tag@resource", "KEYWORD"),
SET_PREFERENTIAL_PRODUCT("set_preferential_product@resource", "KEYWORD"),
enum怎么用CANCEL_PREFERENTIAL_PRODUCT("cancel_preferential_product@resource", "KEYWORD"),
SET_ADMATCH("set_admatch@resource", "KEYWORD"),
CANCEL_ADMATCH("cancel_admatch@resource", "KEYWORD"),
MODIFY_COST_LIMIT("modify_cost_limit@resource", "ACCOUNT"),
MODIFY_BALANCE_ALERT_AMOUNT("modify_balance_alert_amount@resource", "ACCOUNT"),
MODIFY_BALANCE_ALERT_EMAIL("modify_balance_alert_email@resource", "ACCOUNT"),
MODIFY_BALANCE_ALERT_PHONE("modify_balance_alert_phone@resource", "ACCOUNT"),
ACCEPT_AGREEMENT("accept_agreement@resource", "ACCOUNT"),
START_ACCOUNTS_TATUS("start_account_status@resource", "ACCOUNT"),
STOP_ACCOUNT_STATUS("stop_account_status@resource", "ACCOUNT"),
SETTING_ACCOUNT_LANGUAGE("setting_account_language@resource", "ACCOUNT"),
CANCEL_ACCOUNT_LANGUAGE("cancel_account_language@resource", "ACCOUNT"),
ALLOW_RECOMMEND_KEYWORD("allow_recommend_keyword@resource", "ACCOUNT"),
DONT_ALLOW_RECOMMEND_KEYWORD("dont_allow_recommend_keyword@resource", "ACCOUNT"),
UNFREEZE_CUST("unfreeze_cust@resource", "ACCOUNT"),
ADD_OFFER("add_offer@resource", "PRODUCT"),
CLEAN_PRODUCT("clean_product@resource", "PRODUCT"),
DELETE_OFFER("delete_offer@resource", "PRODUCT"),
ADD_ALL_OFFER("add_all_offer@resource", "PRODUCT"),
DELETE_ALL_OFFER("delete_all_offer@resource", "PRODUCT"),
ADD_BEST_MATCH_OFFER("add_best_match_offer@resource", "PRODUCT"),
CREAT_TAG("create_tag@resource", "TAG"),
MODIFY_TAG_NAME("modify_tag_name@resource", "TAG"),
DELETE_TAG("delete_tag@resource", "TAG"),
INSERT_AUTH_MANAGE("insert_authority@resource", "AUTHORITY"),
MODIFY_AUTH_MANAGE("modify_authority@resource", "AUTHORITY"),
CANCEL_AUTH_MANAGE("cancel_authority@resource", "AUTHORITY"),
START_QUICK_PROMOTION("start_quick_promotion@resource", "QUICKPRROMOTION"),
STOP_QUICK_PROMOTION("stop_quick_promotion@resource", "QUICKPRROMOTION"),
UPDATE_QUICK_PRICE("update_quick_price@resource", "QUICKPRROMOTION"),
private OperationType(String resourceKey, String type) {
}
}
private final String resourceKey;
private final String type;
public String getResourceKey() {
return resourceKey;
}
public String getType() {
return type;
}
public static OperationType getOperationType(String name) {
return OperationType.valueOf(name);
}
}
上⾯定义了⼀个java枚举类型的类,构造函数的两个参数很重要,分别对应枚举实例的两个参数。下⾯的测试代码,可以明确告诉你如何使⽤java枚举获取对应的信息。
public class Test {
public static void main(String[] args) {
OperationType operationType = OperationType("ADD_STOP_KEYWORD");
System.out.println("Type():" + Type());
System.out.println("ResourceKey():" + ResourceKey());
}
}
上例的输出结果如下:
<strong&Type():KEYWORD
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论