What is enumeration and enumeration?

Source: Internet
Author: User

What is enumeration and enumeration?
What is the Java Enumeration type? My answer: enumeration is A set defined by ourselves. For example, A = {1, 2, 3} is the set we have learned in mathematics }, when we want to use the set A, we can only use the three elements 1, 2, and 3 in the Set A, which are not the elements in. Similarly, enumeration is similar to this set. When we define an enumeration type, we declare that it contains several elements. When we use this enumeration, we can only use some of its elements. If no element is available, the system reports an error! Another feature of enumeration is that it can represent a value. For example, the value of the first defined element is 0, and each enumeration element starts from 0 and increases one by one. At this time, this value also represents them, which is equivalent to each element with two names. Often used: We need to get a value from another function, but this value can only be within a range. In this case, we can use enumeration to define this range. Restrict the values that can be passed in by another function, only the elements in the enumeration type. The following describes how to use the enumeration type in java: 1. We can define an enumeration type public enum Color {Red, Blue, Black, Green ;}and then use: Color myColor = Color. red 2. The Enumeration type provides two useful static methods: values () and valueOf (). We can easily use them, for example, for (Color c: Color. values () System. out. println (c); 3. For example: package Enum; public class ColorTest {public static void main (String [] args) {Color myColor = Color. red; System. out. println (myColor ); System. out. println ("........................ "); for (Color color: Color. values () {System. out. println (color) ;}}4. There are five types in switch () brackets: char, short, int, long, and enum (Enumeration type) enumeration can be used as a method parameter to make different results by making corresponding judgments in the method. Package Enum; public class EnumTest01 {public static void doWhat (OP op) {switch (op) {case TURE_LEFT: System. out. println ("convert to u24038); break; case TURE_RIGHT: System. out. println ("convert to u21491); break; case SHOOT: System. out. println (" u20987"); break ;}} public static void main (String [] args) {EnumTest01.doWhat (OP. SHOOT) ;}} enum OP {TURE_LEFT, TURE_RIGHT, SHOOT;} output result: shooting 5. Defining enumeration types is essentially defining a category, but many details are completed by the compiler, so to some extent The enum keyword acts like a class or interface6. When we use "enum" to define the enumeration type, the type we defined actually inherits from java. lang. the Enum type, while the enumerated members are actually an Instance of the enumeration type we define. They are all preset as final, so we cannot change them. They are also static members, so we can use them directly through the type name. Of course, most importantly, they are all public ). That is, each Enumeration type we define inherits from the java. lang. Enum class, and each member in the enumeration is public static final by default. Each enumeration member is actually an Instance of the enumeration type we define ). In other words, after defining an enumeration type, you can determine at the time of compilation how many instances are available for this enumeration type. During the runtime, we can no longer use this enumeration type to create new instances. These instances have been completely determined during compilation. So in: Color myColor = Color. Red, there is no new Color (). Instead, assign package Enum; public enum Coin {banjin ("Half u26020"), baliang ("Eight u20004"), qingyuan (" u28170 "), liping ("Li u-31731"); private String name; Coin (String name) {this. name = name;} public void setName (String name) {this. name = name;} public String getName () {return name;} public static void main (String [] args) {Coin coin = Coin. baliang; System. out. println (coin. getName () ;}} output:, via coin. ordinal () to obtain the value represented by enumeration, That is, each enumeration element starts from 0 and adds 8, // check permissions one by one, such as the permission of a manager, employee, or customer to use a tool, enumeration is generally used for processing because this can be used to control the number of input judgment parameters that can only be the enumeration type, thus limiting the input parameters. Package Enum; public class AccessControl {public static boolean checkRight (AccessRight access) {if (access = AccessRight. MANAGER) {return true;} else if (access = AccessRight. DEPARMENT) {return false;} else return false;} public static void main (String [] args) {System. out. println (checkRight (AccessRight. DEPARMENT) ;}} enum AccessRight {MANAGER, DEPARMENT, EMPLOYEE;} in this way, only AccessRight parameters can be passed. Other parameters are invalid. 9. static <T extends Enum <T> T valueOf (Class <T> enumType, String name) returns the enumerated constant of the specified enumeration type with the specified name. You can use AccessRight access = AccessRight. valueOf ("MANAGER") in this way; // This element can be returned as long as there is a MANAGER element in AccessRight, which converts the String type to the AccessRight type.

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.