Adding a new method to a Java enumeration type

Source: Internet
Author: User
Tags protected constructor


In addition to not inheriting an enum, it can be seen as a general class. Can even have the main method.
Note: You must first define an enum instance. The end of the instance has a semicolon.


Here's a sample: Returns a descriptive narrative of the instance itself, rather than the default ToString returns the name of the enumeration instance.
public enum Color ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);    Member variable    private String name;    private int index;    Construction Method    Private Color (String name, int index) {        this.name = name;        This.index = index;    }    public static String getName (int index) {    //takes advantage of the values () method of the enumeration itself; for        (Color c:color.values ()) {            if (C.geti Ndex () = = index) {                return c.name;            }        }        return null;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }}

The importance is to be able to invoke the corresponding enumeration member's method to generate the corresponding object, such as the following oftype, can be used:

OFType t = Oftype.hello;

T.newinstance ();



The following is a reflection of the relevant knowledge points in the Floodlight controller

public enum OFType {//The constructor method is defined here.                             There are three parameters 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 (Ofpacketin.class, New instantiable<ofmessage> () {@Over                            Ride public Ofmessage Instantiate () {return new Ofpacketin ();                            }}), Packet_out (Ofpacketout.class, New instantiable<ofmessage> () { @Override Public OfmessaGE instantiate () {return new ofpacketout (); }}), Flow_mod (Offlowmod.class, New instantiable<ofmessage> () {@Overri                            De public ofmessage Instantiate () {return new Offlowmod ();    }});    static oftype[] mapping; Each message type, need the corresponding detailed implementation class protected class<    Extends Ofmessage> clazz; No-reference constructor for each message class protected constructor<    Extends Ofmessage> constructor; Interface Instantiable has a method of initializing an instance.    Create a detailed ofmessage protected instantiable<ofmessage> instantiable;    The value of the message type protected byte type; /** Construction Method * Store Some information about the OpenFlow type, including wire protocol * Type number, length, and Deriv  Ed 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, creates an * instance of the S Pecified 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); Value to enum class mapping}/** * Adds a mapping from type value to OFType enum * * @param i OpenFlow wire protocol Typ E * @param t type */static public void addmapping (byte I, OFType t) {if (mapping = = null) m apping = 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 */ST atic 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 con Structor */public constructor<?

Extends Ofmessage> GetConstructor () {return constructor; }/** * Returns A new instance of the Ofmessage represented by this OFType * @return the new object */P Ublic ofmessage newinstance () {return instantiable.instantiate (); }/** * @return the instantiable * */Public instantiable<ofmessage> getinstantiable () {return I nstantiable; }/** * @param instantiable the instantiable to set */public void setinstantiable (instantiable<ofmessage > instantiable) {this.instantiable = instantiable; }}




Adding a new method to a Java enumeration type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.