Today, in the work of the problem, to render all the enumeration elements on the foreground page, but the foreground page uses velocity, so you need to put the enumeration elements in the map, and then render in the foreground. It is not possible to take an enumeration element out of one and put it, so that you can use a loop to get it done. There is a way to find the enumeration class that fits my needs. Ok not to say that we are one of the main methods of introduction.
First look at the enumeration class defined:
Package com.zkn.newlearn.enums;/** * * @author zkn 2016-07-11 * */public enum EnumTest01 {update (1, "Update"), query (2, "query "), delete (3," delete ");p rivate integer enumvalue;private string enumdesc;private EnumTest01 (integer enumvalue, string ENUMDESC) {this.enumvalue = Enumvalue;this.enumdesc = Enumdesc;} public int GetEnumValue () {return this.enumvalue;} Public String Getenumdesc () {return this.enumdesc;}}
Java.lang.EnumEach enumeration class implicitly inherits the Java.lang.Enum class, and you may ask, how do you know? We can use reflection to parse the next EnunTest01 enum class.
public void Testenummethod () {Class clazz = Enumtest01.class; Class clazzsuper = Clazz.getsuperclass (); if (clazzsuper! = null) System.out.println (Clazzsuper.getname ());}
The output is: Java.lang.Enum. It is for this reason that there is not much inheritance in Java, so enum classes cannot inherit. But interfaces can be implemented.
Public enum EnumTest02 implements sender{; @Overridepublic void Dosend () {//TODO auto-generated method stub}}
Package com.zkn.newlearn.gof.factory;/** * * @author zkn * */public interface Sender {public void Dosend ();}
ValuesMethodBy flipping through the methods in the parent class, you can't find out where this method is. So how is this method produced? Is the compiler added when compiling the enumeration class. We can also get this result by reflection.
public void Testenummethod () {Class clazz = Enumtest01.class; Method[] methods = Clazz.getdeclaredmethods (); for (Method method:methods) {System.out.println (Method.getname ());}}
The output is:
You can see that there is one of the values method.
So what's the use of the values method? By using the values method, we can get all the elements in the enumeration class. Take a look at the example:
System.out.println (Arrays.tostring (Enumtest01.values ()));//loop all enum type for (EnumTest01 test:EnumTest01.values ()) { System.out.println (Test.getenumdesc () +test.getenumvalue ())//system.out.println (Test.name () + " " + Test.ordinal ());}
The output is:. You can use this method when you need to get all the enumeration elements in an enumeration class. ValueOf methodThis method is also compiled at compile time to add to the enumeration class. See above for examples. The function of this method is to get an enumeration class by the name of the enumeration element.
System.out.println (enumtest01.valueof ("UPDATE"). Getenumdesc ());
Corresponding to this is the Java.lang.Enum, which also has a valueof method in the parent class. But the usage of this method is a little bit different. As shown below:
System.out.println (enum.valueof (Enumtest01.class, "DELETE"). Getenumdesc ());
Switch statementFrom java1.6, we can put the enumeration element in the case of the switch statement. Examples are as follows:
public void Testswitchenum () {putswitchenum (enumtest01.delete);} public void Putswitchenum (EnumTest01 enumswitch) {switch (enumswitch) {case DELETE:System.out.println ("This is the DELETE method"); Break;case UPDATE:System.out.println ("This is the UPDATE method"), Break;default:system.out.println ("This is the Query method");
Enumeration element Customization MethodsYou can override methods in an enumeration class by customizing the method in the enumeration element.
Package com.zkn.newlearn.enums;/** * * @author zkn 2016-07-11 * */public enum enumtest02{green{@Overridepublic void g Etinfo () {System.out.println ("This is the green light");} public void Getprint () {System.out.println ("Zhangsan");}},red{@Overridepublic void GetInfo () {System.out.println (" This is a red light ");}},yellow{@Overridepublic void GetInfo () {System.out.println (" This is a yellow light ");}; public void GetInfo () {}}
Why is it possible to do so? Let's take a look at the compiler-compiled class file: We areIt can be understood,
Each enumeration element is an inner subclass of an inherited enumeration class, and every inner subclass contains all the enumeration elements. The following example can be used to substantiate:
Implementing a single caseIt is estimated that this is the simplest way to implement a single case. There is only one enumeration element.
Package com.zkn.newlearn.gof.singleton;/** * Enumeration Implementation Singleton * @author ZKN * */public enum SingletonGofTest04 {singleton;}
Ordinal method
This method is used to represent the order of the elements in the enumeration class, starting at 0. We'll use that in the back.
For (EnumTest01 test:EnumTest01.values ()) {System.out.println (Test.name () + "" +test.ordinal ());}
Name methodThe name method is used to represent the name of the enumeration element, and the names of the enumeration elements that you write. See for example.also:You can write an enumeration class like a Java class, except that the enumeration class cannot integrate other classes. EnummapThere are many kinds of implementation classes for map, Enummap we can see from the name that this map is for enumeration classes. Its key is the enumeration element, and the value is customized. At work we can also use other maps to implement our enumeration requirements, but why use this enummap? Because of its high performance! Why is performance high? Because its interior is maintained with an array of data structures! We can take a look at its source code implementation:Put method
Public V put (K key, V value) { Typecheck (key); int index = key.ordinal (); Object oldValue = Vals[index]; Vals[index] = masknull (value); if (OldValue = = null) size++; Return Unmasknull (OldValue); }
Typecheck is used to check the type of key, because key can only be an enumeration element. The next sentence int index = key.ordinal (); Key.ordinal () This is the ordinal of the enumeration type we said above, and is then used as the subscript of the array and placed in the Vals array. What about the Get method?Get method
Public V get (Object key) { return (Isvalidkey (key)? ) Unmasknull (vals[((enum<?>) key). Ordinal ()]): null); }
Note This sentence: vals[((enum<?>) key). Ordinal ()]. This is not to get the subscript, according to the subscript to get the values in the array?! Remove method
Public V Remove (Object key) { if (!isvalidkey (key)) return null; int index = ((enum<?>) key). ordinal (); Object oldValue = Vals[index]; Vals[index] = null; if (oldValue! = null) size--; Return Unmasknull (OldValue); }
The Remove method is also very simple to implement, that is, the corresponding subscript element into NULL, waiting for GC recovery.
Here we just said enummap more commonly used three methods, if interested students can look at other methods to achieve. An example of using Enummap:
enummap<enumtest01, string> enummap = new enummap<enumtest01, string> (Enumtest01.class); EnumMap.put ( Enumtest01.delete, "DSDSD"); Enummap.put (Enumtest01.update, "QQQQQQ"); for (map.entry<enumtest01, String> Entry : Enummap.entryset ()) {System.out.println (Entry.getvalue () + Entry.getkey (). Getenumdesc ());}
EnumsetEnumset This is a collection for manipulating enums, an abstract class that has two inheriting classes: Jumboenumset and Regularenumset. When used, you need to make an enumeration type. It is also characterized by very fast speed, why is it fast? Because each time an add is taken, each enumeration value is only one of the long integers. We can look at the source code to see its implementation:Add method
Public boolean Add (E e) { typecheck (e); Long oldelements = elements; Elements |= (1L << ((enum<?>) e). ordinal ()); return elements! = oldelements; }
From which we can see that it is first to move the sequence number of the enumerated type to a long integer, and then the long integer or.of methodThe of method has several overloaded methods that create an enumerated set that initially contains the specified element.
enumset<enumtest01> enumsets = Enumset.of (Enumtest01.delete);
AllOfCreates an enumeration set that contains all the elements of the specified element type.
enumset<enumtest01> enumsets = Enumset.allof (Enumtest01.class);
Range methodCreates a set of the specified range.
enumset<enumtest01> enumsets = Enumset.range (enumtest01.delete,enumtest01.update);
Noneof methodCreates an empty set that specifies an enumeration type.
enumset<enumtest01> Enumset = enumset.noneof (Enumtest01.class); Enumset.add (Enumtest01.delete); EnumSet.add ( Enumtest01.update); for (iterator<enumtest01> it = Enumset.iterator (); It.hasnext ();) {System.out.println (It.next (). Getenumdesc ());} for (EnumTest01 enumtest:enumset) {System.out.println (Enumtest.getenumdesc () + " ... ");}
CopyOf
Creates a set and copy an enumeration element from the collection that is passed in.
enumset<enumtest01> enumsets = enumset.copyof (Enumset);
Other methods don't say much, but you can think about the implementation of the next method in Enumsetiterator.
Okay, so let's get to the introduction to enumerations. If you have questions, you can comment on the exchange.
Explanation of the usage of Enum, Enummap, Enumset