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

最新文章

  1. iOS之设置头像(访问系统相册、本地上传)
  2. firefox安装firebug\XPath Checker
  3. 面试准备 - 最大堆的Csharp实现
  4. MongoDB安装并设置为windows服务以使其开机自启
  5. ZeroMQ(java)中组件间数据传输(Pipe的实现)
  6. 转 Web移动应用调试工具——Weinre
  7. 一个socket发送调试信息的类
  8. fil_space_t
  9. file_put_contents() 图片保存 函数成功之后返回值
  10. VC皮肤库之duilib
  11. 【※索引】mysql索引常用命令
  12. PHPの页面跳转-常见方法
  13. android中操作SQLite注意事项
  14. webpack 的使用1
  15. http常见状态码(转载)
  16. Objective-C Runtime 文档翻译
  17. spring-cloud-feign负载均衡组件
  18. DevExpress GridControl复合表头(多行表头)设置
  19. IIS 字符串过长
  20. ubantu 安装 wget

热门文章

  1. 【洛谷3648/BZOJ3675】[APIO2014]序列分割(斜率优化DP)
  2. Android开发中的SQLite事务处理,即beginTransaction()方法
  3. cglib动态代理(即AOP)
  4. 元素属性的添加删除(原生js)
  5. Python之NumPy实践之数组和矢量计算
  6. C#——工厂模式
  7. java攻城狮之路--复习xml&amp;dom_pull编程
  8. 海盗(Haidao)网店系统最新官方版
  9. Caffe2:添加CUDA路径
  10. iOS UIWebView 访问https绕过证书验证的方法