向Java枚舉類型中加入新方法

來源:互聯網
上載者:User

標籤: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枚舉類型中加入新方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.