Java Basic Part review (VII, Java enumeration type use)

Source: Internet
Author: User

/*** 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.