This is the 12th day of my participation in the More text Challenge. For details, see more text Challenge

Getting Started with Enumeration Classes

Enumeration class definition

Enumeration classes are defined using the enum keyword (which has the same status as the class and interface keywords). A Java source file can have only one publicly decorated enumerated class, and the Java source file must have the same name as the enumerated class. The following to define an enumeration types: public enum TestDemo {SPRING, SUMMER, FALL, WINTER} is very simple.Copy the code

The difference between an enumerated class and a normal class

1. An enumerated class can implement one or more interfaces. Classes defined using EUNM inherit java.lang.Enum by default, not Object, so an enumerated class cannot show inheritance from another parent class. The java.lang.Enum class implements the java.lang.Serializable and java.lang.Comparable interfaces. Non-abstract enumerated classes are final by default, so an enumerated class cannot subclass. 3. Enumeration class constructors can only use the private modifier. If omitted, the system defaults to using the private modifier. 4. Each instance of the enumeration class must display the declaration, each instance separated by commas.Copy the code

Enumerates the methods of a class

The enumeration class defaults to the following methods:

/** * @author Gw_gw * @date 2021/6/12 18:50 * @version 1.0 */ public class TestDemo{public Static void main(String[] args) {// Use the values() method to iterate over the values of the enum class for(Season I: season.values ()){system.out.println (I); }}} / enumeration class represents the four seasons of * * * * / enum Season {/ * * * * of chun xiaqiu WINTER/SPRING, SUMMER, FALL and WINTER. }Copy the code
The ordinal() method finds the index of each enumeration constant, just like an array index. Public class TestDemo{public static void main(String[] args) {Season[] Season = season.values (); For (Season I: Season){system.out.println (I +" index is "+i.ordinal()); }}}Copy the code
The valueOf() method returns an enumeration constant with the specified string value. IllegalArgumentException public class TestDemo{public static void main(String[] args) {// Return the enum with valueOf(), IllegalArgumentException System.out.println(season.valueof ("SPRING")); //error IllegalArgumentException System.out.println(Season.valueOf("asd")); }}Copy the code
The compareTo(E o) method specifies the comparison order of enumerated objects. If the enumeration object returns a positive integer after the enumeration object is specified and before it returns a positive integer, Public class TestDemo{public static void main(String[] args) {// Return -1 System.out.println(Season.SPRING.compareTo(Season.SUMMER)); }}Copy the code
String toString(): returns the name of the enumeration constant. The toString() method is more commonly used. Public class TestDemo{public static void main(String[] args) { System.out.println(Season.SPRING.name()); System.out.println(Season.SPRING.toString()); // Call toString() system.out.println (season.spring); // Call toString() system.out.println (season.spring); }}Copy the code

Enumerates the member variables and constructors of the class

Enumerations can have their own variables, methods, and constructors just like ordinary classes. Constructors can only use private access modifiers and cannot be called from outside. Enumerations can contain either concrete or abstract methods. If the enumeration class has an abstract method, then every instance of the enumeration class must implement it. public class TestDemo{ public static void main(String[] args) { Season season = Season.SPRING; season.print(); }} / enumeration class represents the four seasons of * * * * / enum Season {/ * * * * / SPRING, SPRING, SUMMER, autumn and WINTER SUMMER, FALL and WINTER. private Season(){ System.out.println(this); } public void print(){system.out.println (" this is a season enumeration "); }}Copy the code

The above example you can run by yourself, here is not to give the results of the run, everyone hands-on practice, explore the mystery of Java enumerated classes!