Similarities and differences in Enum and C # in Java

Source: Internet
Author: User

The enumeration type is a new feature of the JDK5.0. Sun introduced a new keyword enum to define an enumeration class. The following is a definition of a typical enumeration type:

Java code public enum color{Red,blue,black,yellow,green}

Obviously, the enum is very much like a special class, in fact the type of the enum declaration definition is a class. These classes are subclasses of the Enum class in the class library (java.lang.enum<e>). They inherit a number of useful methods in this enum. The following is a detailed description of the characteristics of the enumeration classes defined by the enum and their usage. (followed by color examples)

1. The Color enumeration class is a special class whose enumeration value (Red,blue ...) is a color class object (class instance):
Color c=color.red;
And these enumerated values are public static final, the constant way we often define, so the enumeration values in the enumeration class are best capitalized.

2, that is, the enumeration class is class, and of course there are constructors, methods, and data fields in the enumeration type. However, the constructors of an enumeration class differ greatly:
(1) The constructor is invoked only when the enumeration value is constructed.

Java Code enum color{                         red (255,0,0), BLUE (0,0,255), Black (0,0,0), yellow (255,255,0), GREEN (0,255,0);                    // Constructs enumeration values, such as red (255,0,0)                     private color (INT&NBSP;RV,INT&NBSP;GV,INT&NBSP;BV) {                          this.redvalue=rv;                          this.greenValue=gv;                          this.bluevalue =bv;                         }                            public string tostring () {  // Custom Public Methods                     return super.tostring () + "(" +redvalue+ "," +greenvalue+ "," +bluevalue+ ")";                          }                                    private int  redvalue;  //custom data fields, private for encapsulation.                         private int greenvalue;                         private int bluevalue;         }  

Enum color{

RED (255,0,0), BLUE (0,0,255), Black (0,0,0), yellow (255,255,0), GREEN (0,255,0);

Constructs an enumeration value, such as Red (255,0,0)

Private Color (int rv,int gv,intbv) {

THIS.REDVALUE=RV;

THIS.GREENVALUE=GV;

THIS.BLUEVALUE=BV;

}

Public String toString () {//Custom public method

Returnsuper.tostring () + "(" +redvalue+ "," +greenvalue+ "," +bluevalue+ ")";

}

private int redvalue; Custom data fields, private for encapsulation.

private int greenvalue;

private int bluevalue;

}

(2) The constructor is private only, and the public constructor is never allowed. This guarantees that the external code cannot construct an instance of the enumeration class in a new way. This is perfectly reasonable, because we know that the enumeration value is a constant of public static final. However, the methods and data fields of an enumeration class can allow external access.

Java code public static void Main (String args[]) {//color Colors=new color (100,200,300);                   Wrong Color color=color.red;  SYSTEM.OUT.PRINTLN (color); The ToString () method was invoked}

public static void Main (stringargs[])

{

Color Colors=newcolor (100,200,300); Wrong

Color color=color.red;

SYSTEM.OUT.PRINTLN (color); The ToString () method was invoked

}

3. All enumerated classes inherit the methods of the enum, and we describe them in detail below.
(1) ordinal () method: Returns the order of enumerated values in the enumeration class. This order depends on the order in which the enumeration values are declared.
Color.RED.ordinal (); Return Result: 0
Color.BLUE.ordinal (); return Result: 1
(2) CompareTo () method: An enum implements the Java.lang.Comparable interface, so it can be compared to the order of the specified object. The CompareTo in the enum returns the difference in the order of the two enumerated values. Of course, the premise is that two enumerated values must belong to the same enumeration class, otherwise the ClassCastException () exception is thrown. (Concrete visible source code)
Color.RED.compareTo (Color.Blue); return result-1
(3) VALUES () method: A static method that returns an array containing all the enumerated values.
Color[] Colors=color.values ();
for (Color c:colors) {
System.out.print (c+ ",");
}//return Result: Red,blue,black Yellow,green,
(4) ToString () method: Returns the name of an enumerated constant.
Color c=color.red;
System.out.println (c);//return Result: RED
(5) valueof () Method: This method corresponds to the ToString method and returns an enumerated constant with the specified enumeration type with the specified name.
Color.valueof ("BLUE"); return Result: Color.Blue
(6) Equals () method: Compares the references of two enumerated class objects.

Java Code//JDK Source code: Public Final Boolean equals (Object other) {return this==other; }

JDK Source code:

Public final booleanequals (Object other) {

return this==other;

}


4. Enumeration classes can be used in switch statements.

Java code Color color=color.red;                switch (color) {case RED:SYSTEM.OUT.PRINTLN ("It s RED");                Case BLUE:System.out.println ("It ' s BLUE");        Case BLACK:System.out.println ("It ' s Blue"); }

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.