向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
- 向Java枚举类型中添加新方法
- enum可以添加新方法-枚举类型
- java中枚举类型
- 向Elastix中添加framed类型模块
- 如何向android的framework里添加新类,新方法
- Java中关于枚举类型的使用
- java 中枚举类型的用法
- java中枚举类型Enum的用法
- java中枚举类型的使用
- JAVA中枚举类型的作用
- Java 中枚举类型的使用
- Java中枚举类型的使用
- java中枚举类型的简单使用
- Java中枚举类型的使用
- Java 中枚举类型的使用
- 枚举类型在Java中使用
- Java中Random自定义的枚举类型
- Java中enum枚举类型用法总结
- [好康特报]请妈妈喝杯咖啡!Nespresso母亲节回馈专案起跑
- 一万有找的Xperia M2 价格降低、品质不打折
- SAMSUNG Galaxy S5 SWAROVSKI水晶版亮相 南韩与中国率先开卖
- 2014最近生活
- 任天堂E3发表会 专业赢得粉丝叫好
- 向Java枚举类型中添加新方法
- BZOJ1001(狼抓兔子)
- jQuery Autocomplete 使用详细说明
- 今天我开博啦!
- setenforce 0 写/etc/profile不行
- 宽屏时代来了,要用jquery操作界面了!
- Google推史上最安全电邮加密工具「End-to-End」
- windows+xampp+php5.4下安装memcache使用session的方法
- Openstack-Hadoop