未完 Java: enum 枚举

[url]http://stackoverflow.com/questions/9850525/whats-the-use-of-enum-in-java[/url][quote][b]An enum is an ordinary class with a predefined and enumerable set of instances.[/b]
So, if you know in advance (in compile-time) how many instances a class will have and if the instances are easily enumerable, then use an enum.[/quote]

[b]JLS 8.9. Enums:[/b]
[url]http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9[/url][quote]The direct superclass of an enum type named E is Enum<E>[/quote][b]Java Tutorials - Enum Types:[/b]
[url]http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html[/url][quote]All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.(但是可以 implements 接口)[/quote]

将一个带参 enum 的参数值作为 Spring applicationContext.xml 中 util:map or util:set 的 elements:
[url]http://stackoverflow.com/questions/14072314/inject-the-value-of-an-enum-into-a-property-using-spring[/url][quote]enum 类:

public class EmailEntity {
public enum EmailTags {

SOME_THING("some thing"), //放入 util-set bean 中
ANOTHER_THING("another thing"),
YET_ANOTHER_THING("yet another thing"); //放入 util-set bean 中

private final String value;

private EmailTags(String value) { this.value = value; }

public String getValue() { return value; }

}
}
applicationContext.xml:

<util:set id="enumValueSet" set-class="java.util.HashSet">
<bean factory-bean="someThing" factory-method="getValue" />
<bean factory-bean="yetAnotherThing" factory-method="getValue" />
</util:set>

<util:constant id="someThing" static-field="com.package.EmailEntity$EmailTags.SOME_THING" />
<util:constant id="yetAnotherThing" static-field="com.package.EmailEntity$EmailTags.YET_ANOTHER_THING" />
使用 util-set bean:

@Service("xService")
public class XServiceImpl implements XService {
@Resource
private Set<String> enumValueSet;
}
util-set enumValueSet 中放入的是两个字符串:
[color=yellow]some thing
yet another thing[/color][/quote]


A demo of with-value enum:
[url]http://stackoverflow.com/questions/1080904/how-can-i-lookup-a-java-enum-from-its-string-value[/url]
public enum Day { 

MONDAY("M"), TUESDAY("T"), WEDNESDAY("W"),
THURSDAY("R"), FRIDAY("F"), SATURDAY("Sa"), SUNDAY("Su"), ;

private final String abbreviation;

// Reverse-lookup map for getting a day from an abbreviation
private static final Map<String, Day> lookup = new HashMap<String, Day>();

static {
for (Day d : Day.values()) {
lookup.put(d.getAbbreviation(), d);
}
}

private Day(String abbreviation) {
this.abbreviation = abbreviation;
}

public String getAbbreviation() {
return abbreviation;
}

public static Day fromAbbreviation(String abbreviation) {
return lookup.get(abbreviation);
}
}



getNext & cyclic getNext in Java Enum:
[url]http://digitaljoel.nerd-herders.com/2011/04/05/get-the-next-value-in-a-java-enum/[/url]


srcs:
java enum(枚举)使用详解 + 总结:
[url]http://www.cnblogs.com/hemingwang0902/archive/2011/12/29/2306263.html[/url]
常量接口 vs 枚举常量类:
[url]http://caerun.iteye.com/blog/458439[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值