向Java枚举类型中加入新方法


除了不能继承enum之外,可将其看做一个常规类。甚至能够有main方法。
注意:必须先定义enum实例。实例的最后有一个分号。


以下是一个样例:返回对实例自身的描写叙述,而非默认的toString返回枚举实例的名字。
public enum Color {
	RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
    // 成员变量
    private String name;
    private int index;

    // 构造方法
    private Color(String name, int index) {
        this.name = name;
        this.index = index;
    }

    public static String getName(int index) {
    	//利用了枚举自身的values()方法;
        for (Color c : Color.values()) {
            if (c.getIndex() == index) {
                return c.name;
            }
        }
        return null;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

重要性在于:能够调用对应枚举成员的方法来生成对应的对象,比方以下的OFType,能够这样使用:

OFType t = OFType.HELLO;

t.newInstance();



以下是Floodlight controller中相关知识点的体现

public enum OFType {
    //这里自己定义构造方法。有三个參数
    HELLO               (0, OFHello.class, new Instantiable<OFMessage>() {
                            @Override
                            public OFMessage instantiate() {
                                return new OFHello();
                            }}),
    ERROR               (1, OFError.class, new Instantiable<OFMessage>() {
                            @Override
                            public OFMessage instantiate() {
                                return new OFError();
                            }}),
 
    PACKET_IN           (10, OFPacketIn.class, new Instantiable<OFMessage>() {
                            @Override
                            public OFMessage instantiate() {
                                return new OFPacketIn();
                            }}),

    PACKET_OUT          (13, OFPacketOut.class, new Instantiable<OFMessage>() {
                            @Override
                            public OFMessage instantiate() {
                                return new OFPacketOut();
                            }}),
    FLOW_MOD            (14, OFFlowMod.class, new Instantiable<OFMessage>() {
                            @Override
                            public OFMessage instantiate() {
                                return new OFFlowMod();
                            }});

    static OFType[] mapping;

    //每一个消息类型,都须要相应的详细实现类
    protected Class<? extends OFMessage> clazz;

    //每一个消息类的无參构造器
    protected Constructor<? extends OFMessage> constructor;

    //接口 Instantiable 有一个初始化实例的方法。创建详细的OFMessage
    protected Instantiable<OFMessage> instantiable;

    //消息类型的值
    protected byte type;

    /**构造方法
     * Store some information about the OpenFlow type, including wire protocol
     * type number, length, and derived class
     *
     * @param type Wire protocol number associated with this OFType
     * @param requestClass The Java class corresponding to this type of OpenFlow message
     * @param instantiator An Instantiator<OFMessage> implementation that creates an
     *          instance of the specified OFMessage
     */
    OFType(int type, Class<?

extends OFMessage> clazz, Instantiable<OFMessage> instantiator) { this.type = (byte) type; this.clazz = clazz; this.instantiable = instantiator; try { this.constructor = clazz.getConstructor(new Class[]{}); } catch (Exception e) { throw new RuntimeException("Failure getting constructor for class: " + clazz, e); } OFType.addMapping(this.type, this); //值到枚举类的映射 } /** * Adds a mapping from type value to OFType enum * * @param i OpenFlow wire protocol type * @param t type */ static public void addMapping(byte i, OFType t) { if (mapping == null) mapping = new OFType[32]; OFType.mapping[i] = t; } /** * Remove a mapping from type value to OFType enum * * @param i OpenFlow wire protocol type */ static public void removeMapping(byte i) { OFType.mapping[i] = null; } /** * Given a wire protocol OpenFlow type number, return the OFType associated * with it * * @param i wire protocol number * @return OFType enum type */ static public OFType valueOf(Byte i) { return OFType.mapping[i]; } /** * @return Returns the wire protocol value corresponding to this OFType */ public byte getTypeValue() { return this.type; } /** * @return return the OFMessage subclass corresponding to this OFType */ public Class<? extends OFMessage> toClass() { return clazz; } /** * Returns the no-argument Constructor of the implementation class for * this OFType * @return the constructor */ public Constructor<?

extends OFMessage> getConstructor() { return constructor; } /** * Returns a new instance of the OFMessage represented by this OFType * @return the new object */ public OFMessage newInstance() { return instantiable.instantiate(); } /** * @return the instantiable */ public Instantiable<OFMessage> getInstantiable() { return instantiable; } /** * @param instantiable the instantiable to set */ public void setInstantiable(Instantiable<OFMessage> instantiable) { this.instantiable = instantiable; } }




原文地址:https://www.cnblogs.com/clnchanpin/p/6905613.html