`

未完 Java: enum 枚举

阅读更多
 
http://stackoverflow.com/questions/9850525/whats-the-use-of-enum-in-java
引用
An enum is an ordinary class with a predefined and enumerable set of instances.
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.


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


将一个带参 enum 的参数值作为 Spring applicationContext.xml 中 util:map or util:set 的 elements:
http://stackoverflow.com/questions/14072314/inject-the-value-of-an-enum-into-a-property-using-spring
引用
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 中放入的是两个字符串:
some thing
yet another thing



A demo of with-value enum:
http://stackoverflow.com/questions/1080904/how-can-i-lookup-a-java-enum-from-its-string-value
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:
http://digitaljoel.nerd-herders.com/2011/04/05/get-the-next-value-in-a-java-enum/


srcs:
java enum(枚举)使用详解 + 总结:
http://www.cnblogs.com/hemingwang0902/archive/2011/12/29/2306263.html
常量接口 vs 枚举常量类:
http://caerun.iteye.com/blog/458439
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics