The new JDK1.5 enum keyword is used to define enumeration class.
Enumeration class and normal class differences :
- Use enum defined enumeration class inherits the default java.lang.Enum class;
- Enumeration class constructor can use the private modifier;
- All enumeration class instances must appear listed in the enumeration class (separated; the end). Examples listed in the system will automatically add public static final modification;
- All enumeration class values provides a method which can easily traverse all the enumerated value;
JDK1.5 enumeration class objects can be used as a switch in expression expression, case clause may be used as the name enumeration value, defined as the need to add enumeration class.
If the enumeration is only one member, it can be implemented as a single way of embodiment modes.
Package Anno; public class the Test { public static void main (String [] args) { // here for a target Season Season = Spring Season.SPRING; spring.showInfo (); Season Summer = Season.SUMMER; summer.showInfo () ; Season Autumn = Season.AUTUMN; autumn.showInfo (); Season Winter = Season.WINTER; winter.showInfo (); spring.test (); // every time a same object execution Season.SPRING obtained, is a single-mode embodiment Season spring1 =Season.SPRING; System.out.println (Spring == spring1); } } enum Season the implements the Tmp { // this call has parameters corresponding to the private constructor SPRING ( "spring", "spring" ), SUMMER ( "summer", "summer" ), aUTUMN ( "autumn", "autumn" ), wINTER ( "winter", "wind Ling Lie" ); Private Final String NAME; Private Final String DESC; Private Season (String NAME, String DESC) { the this .NAME = NAME; this.DESC = DESC; } public void showInfo() { System.out.println(this.NAME+":"+this.DESC); } @Override public void test() { // TODO Auto-generated method stub System.out.println("这是实现Tmp的方法"); } } interface Tmp{ void test(); }