向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;
}
}
:
向Java枚举类型中加入新方法的更多相关文章
- Java 枚举类型简介
目录 Java 枚举示例 Java 枚举构造函数 枚举类型是用于定义常量集合的特殊类型,更确切的说,JAVA枚举类型是一种特殊的 java 类.枚举类型可以包含常量.方法等.在 java5 中添加了 ...
- Python中模拟enum枚举类型的5种方法分享
这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下 以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...
- 【转】java枚举类型enum的使用
原文网址:http://blog.csdn.net/wgw335363240/article/details/6359614 java 枚举类型enum 的使用 最近跟同事讨论问题的时候,突然同事提到 ...
- 【转】掌握java枚举类型(enum type)
原文网址:http://iaiai.iteye.com/blog/1843553 1 背景 在java语言中还没有引入枚举类型之前,表示枚举类型的常用模式是声明一组具有int常量.之前我们通常利用 ...
- 转载 java枚举类型enum的使用 (原文地址:http://blog.csdn.net/wgw335363240/article/details/6359614)
java枚举类型enum的使用 最近跟同事讨论问题的时候,突然同事提到我们为什么java中定义的常量值不采用enmu枚举类型,而采用public final static 类型来定义呢?以前我们都是采 ...
- java枚举类型详解
枚举类型是JDK1.5的新特性.显然,enum很像特殊的class,实际上enum声明定义的类型就是一个类.而这些类都是类库中Enum类的子类(java.lang.Enum<E>).它 ...
- Java枚举类型的用法
JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 1.用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fia ...
- Java枚举类型的使用,数值的二进制表示
一.Java枚举类型的使用 首先请看这段代码: package java上课; public class EnumTest { public static void main(String[] arg ...
- java 枚举类型分析
最近做android开发,需要用到枚举值,这样可以连续赋值,我按之前c++那样书写,如下所示: public enum ColorSelect { RED_BAGE = 0, GREEN_BAGE, ...
随机推荐
- 【Codeforces576E_CF576E】Painting Edges(可撤销并查集+线段树分治)
题目 CF576E 分析: 从前天早上肝到明天早上qwq其实颓了一上午MC ,自己瞎yy然后1A,写篇博客庆祝一下. 首先做这题之前推荐一道很相似的题:[BZOJ4025]二分图(可撤销并查集+线段树 ...
- 【知识总结】扩展卢卡斯定理(exLucas)
扩展卢卡斯定理用于求如下式子(其中\(p\)不一定是质数): \[C_n^m\ mod\ p\] 我们将这个问题由总体到局部地分为三个层次解决. 层次一:原问题 首先对\(p\)进行质因数分解: \[ ...
- MySql(二):常见的那些个约束
今天总结一下mysql当中的常见约束吧! 那什么是约束呢?通俗点讲,约束就是限定指定字段的存放规则! ● 主键约束(Primary Key) ● 外键约束(Foreign Key) ● 非空约束(No ...
- Laravel5.1学习笔记15 数据库1 数据库使用入门
简介 运行原生SQL查询 监听查询事件 数据库事务 使用多数据库连接 简介 Laravel makes connecting with databases and running queries e ...
- C#——接口的意义以及与抽象类的区别
接口的意义是什么呢?接口与抽象类又有什么区别?什么情况选择用接口?什么情况选择用抽象类? 接口的意义: 1.实际开发中的约束作用,继承接口的类必须实现接口规定的方法,方便多人开发中的协同,避免随意性. ...
- phpCURL抓取网页内容
参考代码1:<?php // 创建一个新cURL资源 $ch = curl_init(); // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, " ...
- iOS crash log 解析 symbol address = stack address - slide 运行时获取slide的api 利用dwarfdump从dsym文件中得到symbol
概述: 为什么 crash log 内 Exception Backtrace 部分的地址(stack address)不能从 dsym 文件中查出对应的代码? 因为 ASLR(Address spa ...
- 聊聊JS动画库:Velocity.js
前言 又到了炎热的7月,很久没有更新技术文章了,原因是上月月底实习结束,从公司离职.然后最近在弄自己的项目和考驾照,为了下次公司的应聘做准备,送别了女朋友到外地,哩哩啦啦半个月把一切事情都办妥后,还是 ...
- Js—innerHTML和innerText的区别
1.innerHTML属性和innerText属性 都是对元素的一个操作,简单讲,innerHTML可以在某种特定环境下重构某个元素节点的DOM结构,innerText只能修改文本值 在JavaScr ...
- NTP测试1
ntp server A : 10.101.75.8 B : 10.101.75.38 B: [root@r10n16313.sqa.zmf /home/ahao.mah] #cat /etc/ntp ...