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

来源:互联网 发布:阿里云硬盘品牌 编辑:程序博客网 时间:2024/05/15 18:49

除了不能继承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;    }}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 上海社保里生育险暂停参保怎么办? 痔疮手术后大姨妈来了怎么办 微创痔疮术后第五天涨出血怎么办? 肚子胀疼大便拉不出来怎么办 得痔疮了该怎么办昆明东大治 下体痒还没去检查就来月经了怎么办 直肠造口手术后造口肠子突出怎么办 痔疮pph手术瘢痕两年了该怎么办 肛瘘挂线术后六天腹泻了怎么办 刚满月的孩子鼻子不通气怎么办 齐鲁医院挂的号晚了怎么办 手机微信安装后注册失败怎么办 舞蹈基本功胸怎么都转不动怎么办 饥荒手机版第10天遇到的狗怎么办 饥荒海难手游石墙老是放歪怎么办 小燕子从窝里掉下来了怎么办 二岁宝宝晚上睡觉不踏实怎么办 脚扭伤了有点痛但没肿该怎么办 落地扇的机头摇摆的地方坏了怎么办 跌倒在楼梯上右侧肋骨骆上怎么办 1岁3个月害怕自己不敢走路怎么办 苹果手机没开定位丢了怎么办 我和我老婆每天都吵架怎么办 现在在学注册消防师好枯燥怎么办 店铺台阶太高顾客不愿进来怎么办? 上古卷轴5跑步要沉下去怎么办 1岁半宝宝半夜醒来不睡觉怎么办 上古卷轴5不小心偷了东西怎么办 47牙缺失17号长长了怎么办 碎纸机过热件亮了卡住纸了怎么办 汽车买贵了2万多怎么办 宝宝眼皮被蚊子咬肿了怎么办 一岁宝宝撞头咬到舌头有伤口怎么办 二胎快生了老大特别粘人怎么办 生二胎不舍得大宝跟奶奶睡怎么办 怀二胎婆婆不帮忙带孩子怎么办 注册过的高铁用户名忘了怎么办 硕士延期毕业找好的工作怎么办 竞彩足球绑定信用卡提不了现怎么办 qq启动出现问题请卸载重装怎么办 u盘有文件打开后却是空的怎么办