Java Basics Tutorial-enum type definition

Source: Internet
Author: User
Tags define abstract
• JDK1.5 you can then use enum keyword to define an enumeration type with the following syntax structure:

[public] enum Enumeration Name [implements interface list ] {

Enumerating Objects 1 [, Enumeration Object 2] [,... ];

[ member variable 1; ]

[ member variable 2; ]

[ ... ]

[( Static or non-static ) block of code

[ construction method 1]

[ construction method 2]

[ ... ]

[ general method 1]

[ general method 2]

[ ... ]

[ abstract method 1]

[ abstract method 2]

[ ... ]

}

public enum Color {red (1, "red"), Green (2, "green"), Blue (3, "Blue");p rivate int value;private String label;private Color (int va Lue, String label) {this.value = Value;this.label = label;} public int GetValue () {return value;} Public String Getlabel () {return label;}}

You can define abstract objects directly in an enumeration at this point, each enumeration object needs to implement the abstract method, as in the following example

Enum Color {red {public string Getlabel () {return ' red color ';}},green {public string Getlabel () {return ' green color ';}}; Public abstract String Getlabel ();} public class Test {public static void main (string[] args) {for (Color color:Color.values ()) {String label = Color.getlab El (); SYSTEM.OUT.PRINTLN (label);}}}

• enumerate nouns . enumeration of Image : Gets a specified enumeration object, as in the following example:

static t[] values () : Gets all enumerated objects in the custom enumeration, as in the following example

• Enumeration types can be used in switch, as in the following example:

• Theenum class is an abstract class that is the parent of the Java Language enumeration type, that is, the enum keyword custom enumeration type is equivalent to defining a Inheritance Java.lang.Enum the child of the abstract class class.

•Enum class construction method:

Øprotected Enum(String name, int ordinal) : The construction method receives two parameters, one that represents the name of the enumeration, and another that represents the ordinal of the enumeration;

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.