J2SE5.0 enumeration type

xiaoxiao2021-03-06  52

J2SE5.0 enumeration type

晁 晁 攀 Smallnest@163.com

Considering that again, Sun is also added to the Java language in the Java language. In the traditional design, in order to achieve the enumeration of C, C , we generally declare several constants in the class:

Public Static Final String Belle_xishi = "Xi Shi";

Public static final string belle_wangzhaojun = "Wang Zhaojun";

Public Static Final String Belle_Diaochan = "禅";

Public static final string belle_yangguife = "Yang Guifei";

However, doing this question:

1. Not type security: Because it is declared as a String type, you can assign any string, and it is possible that the value of two constants is the same.

2. No namespace: For distinguish between and non-classification, our defined constants are prefixed in Belle.

3. Vulnerability: Once you are compiled, if you want to insert constants, or their order and value are changed, using their programs need to be recompiled.

4. The meaning of the printed value is not obvious.

Now, in the new Java language, you can already use the enumeration type to solve the above problem, we first take a look at how the enumeration type works.

Package com.kuaff.jdk5; public class enumshow1 {ENUM belle {Xi Shi, Zen, Wang Zhaojun, Yang Guifei Belle.values ​​()) {system.out.printf ("Beautiful Name =% S% N", Belle);}}}

A enumerated type Belle is defined here. It four values: Xi Shi, Zen, Wang Zhaojun and Yang Guifei. Note that the value here is Chinese.

All values ​​of the Belle enumeration type can be obtained through the belle.values ​​() method, which returns an array containing the Belle value.

This way we can get enumerated values ​​via belle.getname () or belle.toString ().

Note: This new feature is described here with the PRINTF method and the Foreach type loop. These new features will be introduced in later articles.

In fact, each enumerated value can set multiple parameters, we will expand the above example:

Package com.kuaff.jdk5; public class enumshow2 {ENUM belle {Xi Shi ("Spring and Autumn Warring States", "Fan Wei, Harmony, Poor") ), 貂 禅 ("Three Kingdoms", "Lu Bu, Dong Zhuo), Yang Guifei (" Tang Dynasty "," Li Longji "); Private Final String Empire; Private Final String Mans; Private Final String Mans; Private Belle (String Empire, String Mans) { This.empire = Empire; this.mans = mans;}} public static void main (String [] args) {enumshow2 show = new enumshow2 (); for (belle belle: belle.values ​​()) {system.out.printf ("Beautiful name:% s, life age:% s, beautiful girl:% s% n", belle, belle.empire, belle.mans;}}} In this example, we are every The beauty has added two parameters of a living age and related man.

Here, there must be a construction method such as Belle (String Empire, String Mans). For each enumeration value, you can use it like an object. To get its parameters, you can access it by enumerating objects. If the following is compiled, in fact, the Java compiler also compiles it into a class:

Static Class ENUMSHOW2 $ Belle Extends ENUM

{

...

}

The above two examples can be compiled under Eclipse 3.1M3, and the current Eclipse3.1m3 has reached 97% of JDK5.

Since it is compiled into a class, we will also think, the class can have a method, is the type of enumeration not within the page support method? Yes!

We have expanded the above example, add a method of displaying its life event for each beauty:

Package com.kuaff.jdk5; public class enumshow3 {ENUM belle {Xi Shi ("Spring and Autumn Warring States", "Fan Wei, Practice, Fu") {String Mainevent () {Return "is a parent, marry, and Fan Wei Pansea Lake Elderly; }} And relatives, Qingda bury ";}}, 禅 (" Three Kingdoms "," Lu Bu, Dong Zhuo ") {string mainevent () {return" Red is really water! " Tang Dynasty "," Li Longji ") {string mainevent () {return" hot springs, good people, long-lived temples, but smelling Phoenix Ease. ";}}; Private final string Empire Belle (String Empire, String Mans) {this.empire = Empire; this.mans = mans;} Abstract string mainevent ();} public Static void main (string [] args) {enumshow3 show = new enumshow3 (); for (Belle belle: belle.values ​​()) {system.out.printf ("Beautiful Name:% S, Domestic Year:% s, Related men after beauty:% s% n ", belle, belle.empire, belle.mans; system.out.printf (" related events:% s% n ", belle.mainevent ());}}} In order to add a method String Mainevent () for each beauty, we must also add an abstract method declaration in this enumeration type: Abstract string mainevent ();

Like types, the enumeration type method is called by enumerating objects. Method (...).

转载请注明原文地址:https://www.9cbs.com/read-83314.html

New Post(0)