Enumeration (after JDK1.5)

1, an enumerated type, a special class, its object is fixed limited number.

Although not say enumeration class attributes of an object can not be modified, but under normal circumstances, we enumerate the properties of an object class is not proposed changes

2, how to declare an enumeration type

[Modifier] enum Type {enum 
    constant object list 
} 
[Modifier] enum Type {enum 
    constant object list; 
    other members 
}

  

3, enumeration class features

(1) must be a private constructor

(2) constant object list must be in the first row

Parent (3) java.lang.Enum enumeration class is class, not other class

(4) its toString () method in the parent class has been rewritten, constant default object name returned is not recommended rewritten, but you need it, you need to manually override toString

 

4, enumeration of what type commonly used methods:

(1) int compareTo (Object obj): Because the parent java.lang.Comparable Enum class implements the interface, this method is relatively constant in the order of magnitude of the object

(2) String name (): Returns the constant object name

(3) int oridinal (): returns the constant target sequence number

(4) enumerated type [] values ​​(): returns all objects constants

(5) enumerated type valueOf (String name): Gets a constant object based on constant object name

 

5, switch (expression) type after JDK1.5 adds support for enumeration.

Guess you like

Origin www.cnblogs.com/panyizuoshan/p/11460808.html