Talking about Java enumeration class

Source: Internet
Author: User

First, a section of common enumeration class code structure

 Public enumColor {//Enumerating ObjectsRed (1, "red"), Green (2, "green"), Blue (3, "blue")); //Private member Properties    Private intindex; PrivateString name; //Private Construction Methods    PrivateColor (intindex, String name) {         This. index =index;  This. Name =name; }        //Public Get Methods     Public intGetIndex () {returnindex; }     PublicString GetName () {returnname; }        //to get a static method of an instance     Public StaticColor getinstance (intnum) {color[] values=color.values ();  for(Color c:values) {if(C.getindex () = =num) {                returnC; }        }        Throw NewIllegalArgumentException ("No Such color"); }    }

As you can see from the code above, the common structure of enumerations is: 1. Enumerate instance objects, 2. Add a private member variable if there is a property. 3. The construction method is private and prevents additional instance objects from being created. 4. Only the Get method prevents modifying the property values of the original object. 5, other methods, such as getting an instance of an object based on a property

Let's take a closer look at some of the hidden things in the enumeration class

1. Enum classes are implicitly inherited from the Java.util.Enum class, and if you display an inheritance that is not valid

2. There are some basic methods in the enumeration class, including

  values(): Returns an array of enum instances, and the elements in the array are strictly maintained in the order in which they are declared in the enum.

  name(): Returns the instance name.

  ordinal(): Returns the order of instance declarations, starting at 0.

  getDeclaringClass(): Returns the enum type to which the instance belongs.

  equals(): Determines whether the same object is the same.

  compareTo():比较方法,因为Enum类实现了Comparable和Serializable接口

Look at the following code

Enum class

 Public enum Color {    red,green,blue,yellow,orange;}

Test class

 Public class enumtest {    publicstaticvoid  main (string[] args) {        =  Color.values ();          for (Color c:values) {            System.out.print (c.name ()+ "---");            System.out.print (c.ordinal ()+ "---");            System.out.println (C.getdeclaringclass ());             }}}

Test results

RED---0---class  com.pancras.test.ColorGREEN---1---class  Com.pancras.test.ColorBLUE---2---class  com.pancras.test.ColorYELLOW---3---class Com.pancras.test.ColorORANGE---4---class Com.pancras.test.Color

There are other things you can do to find out.

Reference: https://www.cnblogs.com/jingmoxukong/p/6098351.html

Talking about Java enumeration class

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.