向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;
    }
}

问题:添加新方法只是得到了对于枚举实例不同的字符串描述信息。那么还有其他特殊的重要性吗??

下面是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;
    }
}

向Java枚举类型中添加新方法,布布扣,bubuko.com

时间: 2024-08-02 02:48:20

向Java枚举类型中添加新方法的相关文章

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

除了不能继承enum之外,可将其看做一个常规类.甚至能够有main方法. 注意:必须先定义enum实例.实例的最后有一个分号. 以下是一个样例:返回对实例自身的描写叙述,而非默认的toString返回枚举实例的名字. public enum Color { RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4); // 成员变量 private String n

java 在数据库中添加新信息

package lianxi1; import java.sql.*; import java.util.*; public class lianxi1 { public static void main(String[] args) { //定义Connection Connection conn = null; try { //链接数据库 Class.forName("oracle.jdbc.driver.OracleDriver"); String strUrl ="j

Java枚举类型的用法

JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 1.用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法.而且枚举类型可以帮助我们检测许多的编译失误. 例如: package enumTest; public enum Color { RED,BLANK,YELLOW } 测试代码: package

详解VMware 虚拟机中添加新硬盘的方法

一.VMware新增磁盘的设置步骤 (建议:在设置虚拟的时候,不要运行虚拟机的系统,不然添加了新的虚拟磁盘则要重启虚拟机) 1.选择“VM”----“设置”并打开,将光标定位在“硬盘(SCSI)”这一选项,然后点击下方的“添加”按钮 2.点击下一步,执行下一个步骤 3.选择磁盘类型 4.根据提示,创建一个虚拟的磁盘,并点击下一步 5.设置磁盘大小,点击下一步 6.选择存储位置,最后点击完成 以下是对虚拟机中Linux和widows中对于新的虚拟磁盘的挂载的方式的介绍 二.虚拟机中windows对

如何在caffe中添加新类型的layer

如何在caffe中添加新类型的layer 参考:https://github.com/BVLC/caffe/issues/684 Add a class declaration for your layer to the appropriate one of common_layers.hpp,data_layers.hpp, loss_layers.hpp, neuron_layers.hpp, or vision_layers.hpp. Include an inline implement

java 枚举类型使用总结

注:阅读了effective java 讲诉enum的部分,做下笔记,下文中大部分代码来自effective java书中 枚举类型是指由一组固定的常量组成的合法值得类型. 使用枚举的好处:a,因为没有可以访问的构造器,枚举类型是真正的final; b,枚举类型提供类编译时的类型安全.如:函数中声明一个参数类型为枚举类型的Apple时,那么调用方法时传递到参数上的任何非null对象一定是Apple的值之一, 而是用静态常量就无法保证; c,包含同名常量的多个枚举类型可以和平共处,每个类型都有自己

【转】掌握java枚举类型(enum type)

原文网址:http://iaiai.iteye.com/blog/1843553 1   背景 在java语言中还没有引入枚举类型之前,表示枚举类型的常用模式是声明一组具有int常量.之前我们通常利用public final static 方法定义的代码如下,分别用1 表示春天,2表示夏天,3表示秋天,4表示冬天. Java代码   public class Season { public static final int SPRING = 1; public static final int 

【转】java枚举类型enum的使用

原文网址:http://blog.csdn.net/wgw335363240/article/details/6359614 java 枚举类型enum 的使用 最近跟同事讨论问题的时候,突然同事提到我们为什么java 中定义的常量值不采用enmu 枚举类型,而采用public final static 类型来定义呢?以前我们都是采用这种方式定义的,很少采用enum 定义,所以也都没有注意过,面对突入起来的问题,还真有点不太清楚为什么有这样的定义.既然不明白就抽时间研究下吧. Java 中的枚举

深入理解Java枚举类型(enum)

[版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/71333103 出自[zejian的博客] 关联文章: 理解Java位运算 深入理解Java类型信息(Class对象)与反射机制 本篇主要是深入对Java中枚举类型进行分析,主要内容如下: 理解枚举类型 枚举的定义 枚举实现原理 枚举的常见方法 Enum抽象类常见方法 编译器生成的Values方法与ValueOf方法 枚举与Clas