In addition to not inheriting an enum, you can consider it a general class. You can even have a main method.
Note: an instance of an enum must be defined first, with a semicolon at the end of the instance.
The following is an example: Returns a description of the instance itself, not the default ToString, which returns the name of the enumeration instance.
The public enum Color {red
("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;
}
The public static String getName (int index) {
//utilizes the values () method of the enumeration itself; for
(Color c:color.values ()) {
if (c.get Index () = = index) {return
c.name
}
}
return null;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
}
The importance is that the corresponding enumeration member can be invoked to generate the corresponding object, such as the following oftype, which can be used:
OFType t = Oftype.hello;
T.newinstance ();
The following is the embodiment of the relevant knowledge points in Floodlight controller
public enum OFType {//Here the custom constructor method has three parameters HELLO (0, Ofhello.class, new instantiable<ofmessage> ()
{@Override public ofmessage instantiate () {
return new Ofhello (); }), ERROR (1, Oferror.class, new instantiable<ofmessage> () {@Overrid
E public Ofmessage Instantiate () {return new Oferror ();
}), Packet_in (Ofpacketin.class, New instantiable<ofmessage> () {
@Override public Ofmessage Instantiate () {
return new Ofpacketin (); }), Packet_out (Ofpacketout.class, New instantiable<ofmessage> () {@O
Verride Public Ofmessage Instantiate () {return new ofpacketout (); }), Flow_mod (Offlowmod.class, New instantiable<ofmessage> () {@ Override public Ofmessage Instantiate () {return new Offlowmod
();
}});
static oftype[] mapping; Each message type needs to correspond to the specific implementation class protected class<?
Extends Ofmessage> clazz; The parameterless constructor for each message class protected constructor<?
Extends Ofmessage> constructor;
Interface Instantiable has a method of initializing an instance to create a specific ofmessage protected instantiable<ofmessage> instantiable;
The value of the message type is protected byte type; /** Construction Method * Store Some information about the OpenFlow type, including wire protocol * Type number, length, and Der Ived class * * @param type Wire protocol number associated with this OFType * @param requestclass the Java Class COrresponding to this type of the OpenFlow message * @param instantiator a instantiator<ofmessage> implementation T Hat creates an * instance of the specified ofmessage/OFType (int type, class<? extends Ofmessa
Ge> 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 the mapping of the Enumeration class}/** * Adds a mapping from type value to OFType enum * * @param i OpenFlow wire PROTOC
OL 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 PU
Blic void removemapping (byte i) {oftype.mapping[i] = null; }/** * Given a wire protocol OpenFlow type number, return the OFType associated * with it * * @p Aram I wire protocol number * @return OFType enum type */static public OFType valueof (Byte i) {RE
Turn oftype.mapping[i]; /** * @return Returns The Wire protocol value corresponding to this OFType */public byte Gettypevalu
E () {return this.type; /** * @return Return of 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 th E 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 () {RE
Turn instantiable; }/** * @param instantiable the instantiable to set */public void setinstantiable (INSTANTIABLE<OFME
Ssage> instantiable) {this.instantiable = instantiable; }
}
: