/*** Enumeration in Java (enum) refers to a sorted list of items that are packaged into a single entity. An instance of an enumeration can use the value of any single item in an enumerated item list. * Enumerations are widely used in various languages, often to denote quantities such as color, manner, category, State, etc., which are limited in number, form discrete, and express in a very definite way. * Java starts with JDK5 and introduces support for enumerations. * * * * * The new enumeration in JDK5 perfectly solves the problem of using constants to represent discrete quantities, greatly enhancing the readability, ease of use and maintainability of the program, and expands on this basis so that it can be used like a class, and it is a step up for the representation of discrete quantities in Java. Therefore, if in Java you need to represent a limited number of colors, patterns, categories, states, and so on, the form is discrete, and the expression is extremely clear, you should discard the practice of constant representation and enumerate as the first choice. * @authorDyq **/ Public classEnumtest { Public Static voidMain (string[] args) { Day day=Day.monday; System.out.println (day); Typeenum Typeenum=Typeenum.video; System.out.println (Typeenum.name); System.out.println (Typeenum.value); }}enumday{MONDAY, Tuesday, Wednesday, Thursday, FRIDAY, SATURDAY , SUNDAY}enumtypeenum {VIDEO (1, "video"), Audio (2, "audio"), Text (3, "text"), Image (4, "image")); intvalue; String name; Typeenum (intvalue, String name) { This. Value =value; This. Name =name; } Public intGetValue () {returnvalue; } PublicString GetName () {returnname; } }
/** * Enumeration (enum) in Java refers to a sorted list of items that are packaged into a single entity. An instance of an enumeration can use the value of any single item in an enumerated item list. * Enumerations are widely used in various languages, often to denote quantities such as color, manner, category, State, etc., which are limited in number, form discrete, and express in a very definite way. * Java starts with JDK5 and introduces support for enumerations. * * * * * The new enumeration in JDK5 perfectly solves the problem of using constants to represent discrete quantities, greatly enhancing the readability, ease of use and maintainability of the program, and expands on this basis so that it can be used like a class, and it is a step up for the representation of discrete quantities in Java. Therefore, if in Java you need to represent a limited number of colors, patterns, categories, states, and so on, the form is discrete, and the expression is extremely clear, you should discard the practice of constant representation and enumerate as the first choice. * @author Dyq * */public class Enumtest {public static void main (string[] args) {Day Day =day.monday; System.out.println (day); Typeenum typeenum = Typeenum.video; System.out.println (Typeenum.name); System.out.println (Typeenum.value);}}
Enum Day{monday, Tuesday, Wednesday, Thursday, FRIDAY, SATURDAY, SUNDAY}enum typeenum {video (1, "video"), AUDIO ( 2, "audio"), Text (3, "text"), Image (4, "image"); int value; String name; Typeenum (int value, String name) {this.value = value; THIS.name = name; } public int GetValue () {return value; } public String GetName () {return name; } }
Java Basic Part review (VII, Java enumeration type use)