Java enum Class enum summary

Source: Internet
Author: User

Enumeration definition:

An enum is a data type in a computer programming language. Enumeration types: In real-world problems, some variables are scoped to a limited range . For example, there are only seven days in one weeks, 12 months in a year, six courses per week in a class, and so on. If these quantities are described as integers, the character type or other types are obviously inappropriate. For this reason, the C language provides a type called Enum. all possible values are enumerated in the definition of the "enum" type , and the variable value that is described as the "enum" type cannot exceed the defined range . It should be stated that an enumeration type is a basic data type, not a constructed type , because it can no longer be decomposed into any basic type.

Summary of enumeration classes in Java:

1. Enumerations are a special type of data .

2. Enumeration contains enumeration constants and general quantities .

3. Each enumeration constant is an instance object of the current enumeration type.

Look at the code:

1  Public classDemo {2 3      Public Static voidMain (string[] args) {4Person Zhangsan =Person.zhangsan;5Zhangsan.setage (25);6System.out.println (Zhangsaninstanceofperson);//true7     }8 9 }Ten  One enumPerson { A  -Zhangsan ("Zhangsan"), Lisi ("Lisi");//to define enumeration constants with default values -  the     /** - * Normal data type -      */ -     PrivateAtomicinteger count =NewAtomicinteger (); +     Private intAge ; -     PrivateString name; +  A     /** at * Constructors for enumerations -      * @param Age -      * @paramname -      */ -     PrivatePerson (intAge , String name) { -          This. Age =Age ; in          This. Name =name; -System.out.println ("Init count:" +count.addandget (1)); to     } +  -      the     /** *      * @return The Age $      */Panax Notoginseng      Public intGetage () { -         returnAge ; the     } +  A      the     /** +      * @paramAge-The age-to set -      */ $      Public voidSetage (intAge ) { $          This. Age =Age ; -     } -  the      -     /**Wuyi      * @returnThe name the      */ -      PublicString GetName () { Wu         returnname; -     } About  $      -     /** -      * @paramname the name to set -      */ A      Public voidsetName (String name) { +          This. Name =name; the     } -  $}

Java enum Class enum summary

Related Article

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.