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