標籤:this 構造 自己 packet 實現 eval [] ofmessage new
除了不能繼承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枚舉類型中加入新方法