Enumeration of new Java features

Source: Internet
Author: User

One, multiple cases mode

Multi-Example Mode implementation method one

package cn4;public class multiinstance {// Provides a limited number of instantiated objects public static final multiinstance front = new multiinstance (); Public static final multiinstance behind = new multiinstance ();p ublic  Static final multiinstance left = new multiinstance ();p ublic static  Final multiinstance right = new multiinstance ();//Construction Method Privatization private multiinstance () {}}package cn4;public class test {public static void main (String[]  args)  {MultiInstance front = MultiInstance.FRONT; multiinstance behind = multiinstance.behind; multiinstance left = multiinstance.left; multiinstance right = multiinstance.right; SYSTEM.OUT.PRINTLN (front); System.out.println (behind); System.out.println (left); System.out.println (right);}} 

[Email protected]

[Email protected]

[Email protected]

[Email protected]


Multi-Example Mode implementation method two

PACKAGE CN4;PUBLIC CLASS MULTIINSTANCE2 {//provides a limited number of instantiated objects public static final  Multiinstance2 front = new multiinstance2 ("front");p ublic static final  Multiinstance2 behind = new multiinstance2 ("after");p ublic static final  Multiinstance2 left = new multiinstance2 ("left");p ublic static final  Multiinstance2 right = new multiinstance2 ("right");p rivate string name;// Construction method Privatization Private multiinstance2 (String name) {this.name = name;} Public string getname () {return this.name;}} Package cn4;public class test2 {public static void main (String[] args)  {MultiInstance2 front = MultiInstance2.FRONT; multiinstance2 behind = multiinstance2.behind; multiinstance2 left = multiinstance2.left; Multiinstance2 right = multiinstance2.right; SYSTEM.OUT.PRINTLN (front); System.out.println (behind); System.out.println (left); System.out.println (right); System.out.println (Front.getname ()); System.out.println (Behind.getname ()); System.out.println (Left.getname ()); System.out.println (Right.getname ());}}

[Email protected]

[Email protected]

[Email protected]

[Email protected]

Ago

After

Left

Right


Multi-Example Mode implementation method three

PACKAGE CN4;PUBLIC ABSTRACT CLASS MULTIINSTANCE3 {//provides a limited number of instantiated objects Public static  final multiinstance3 front = new multiinstance3 ("front") {@Overridepublic  void  show ()  {system.out.println ("This is front Oh");}; Public static final multiinstance3 behind = new multiinstance3 ("after") {@ Overridepublic void show ()  {system.out.println ("This is behind Oh");}; Public static final multiinstance3 left = new multiinstance3 ("left") {@ Overridepublic void show ()  {system.out.println ("Left this is side Oh");}; Public static final multiinstance3 right = new multiinstance3 ("right") {@ Overridepublic void show ()  {system.out.println ("This is behind Oh");}; private string name;//Construction Method Privatization Private multiinstance3 (String name) {this.name =  Name;} Public string getname () {return this.name;} public abstract void  Show ();} Package cn4;public class test3 {public static void main (String[] args)  {MultiInstance3 front = MultiInstance3.FRONT; multiinstance3 behind = multiinstance3.behind; multiinstance3 left = multiinstance3.left; multiinstance3 right = multiinstance3.right; SYSTEM.OUT.PRINTLN (front); System.out.println (behind); System.out.println (left); System.out.println (right); System.out.println (Front.getname ()); System.out.println (Behind.getname ()); System.out.println (Left.getname ()); System.out.println (Right.getname ()); Front.show (); Behind.show (); Left.show (); Right.show ();}}

[Email protected]

[Email protected]

[Email protected]

[Email protected]

Ago

After

Left

Right

This is the front!

This is the back!

Left, this is the side.

This is behind.


But it would be too much trouble if we wrote multiple patterns. The JDK provides an enumeration for us.

A corresponding enumeration is implemented according to the pattern of multiple cases

Package Cn4;public enum Color {red,green,blue;} Package Cn4;public class ColorTest1 {public static void main (string[] args) {Color red = color.red; Color green = Color.green; Color blue = Color.Blue; SYSTEM.OUT.PRINTLN (red); SYSTEM.OUT.PRINTLN (green); SYSTEM.OUT.PRINTLN (blue);}}

RED

GREEN

BLUE


Implementation of two corresponding enumeration two according to multi-example mode

Package Cn4;public enum Color2 {red ("red"), Green ("green"), Blue ("Blue");p rivate string name;private Color2 (string name) { THIS.name = name;} Public String GetName () {return this.name;}} Package Cn4;public class ColorTest2 {public static void main (string[] args) {Color2 red = color2.red; Color2 green = Color2.green; Color2 blue = Color2.blue; SYSTEM.OUT.PRINTLN (red); SYSTEM.OUT.PRINTLN (green); SYSTEM.OUT.PRINTLN (blue); System.out.println (Red.getname ()); System.out.println (Green.getname ()); System.out.println (Blue.getname ());}}

RED

GREEN

BLUE

Red

Green

Blue


Three corresponding enumerations are implemented according to the pattern of multiple cases

Package Cn4;public enum Color3 {red ("red") {@Overridepublic void Show () {System.out.println ("This is Red"),}},green ("green") {@ Overridepublic void Show () {System.out.println ("This Is Green"),}},blue ("Blue") {@Overridepublic void Show () {System.out.println ( "This is Blue");}; private string Name;private Color3 (string name) {this.name = name;} Public String GetName () {return this.name;} public abstract void Show ();

RED

GREEN

BLUE

Red

Green

Blue

It's red.

It's green.

It's blue.


Ii. Considerations for Enumeration

Define enum classes to use the keyword enum.

All enumeration class densities are subclasses of enumerate.

The first line of an enumeration class must be an enumeration item, and the semicolon after the last enumeration item can be omitted, but the semicolon cannot be omitted if there is something else in the enumeration class. It is recommended not to omit.

An enumeration class can have a constructor, but it must be private, and it defaults to private. The use of enumeration items is special: Enumerations ("").

An enumeration class can also have an abstract method, but an enumeration item must override the method.

Enumeration can be used in a switch statement.


Several common methods of enumeration class

int CompareTo (E o)

String name ()

int ordinal ()

String toString ()

<T> T valueOf (class<t> type,string name)

VALUES ()

Although this method is not found in the JDK document, each enumeration class has that method, and it is convenient to traverse all enumerated values of the enumeration class

Package Cn4;public enum Color {red,green,blue;}
Package cn4;public class colordemo {public static void main (String[]  args)  {//int compareto (E o)   Compare this enumeration with the order of the specified objects color red = color.red; color green = color.green; color blue = color.blue; System.out.println (Red.compareto (red));//0system.out.println (Red.compareto (green));//-1system.out.println ( Blue.compareto (red));//2system.out.println ("-------------------------");//string name ()   Returns the name of this enumeration constant, Declare it in its enumeration declaration. System.out.println (Color2.RED.name ());//redsystem.out.println (Color2.GREEN.name ());//greensystem.out.println ( Color2.BLUE.name ());//bluesystem.out.println ("-------------------------");//int ordinal ()   Returns the ordinal of the enumeration constant System.out.println (Color3.RED.ordinal ());//0system.out.println (Color3.GREEN.ordinal ());// 1system.out.println (Color3.BLUE.ordinal ());//2system.out.println ("-------------------------");//string  ToString ()   Returns the name of the enumeration constant System.out.println (Color3.RED.toString ());//REDSystem.out.println (Color3.GREEN.toString ());//greensystem.out.println (Color3.BLUE.toString ());// BLUESystem.out.println ("-------------------------");//<t> t valueof (Class<t> type, String name)   Returns the enumeration constant of the specified enumeration type with the specified name color color = enum.valueof (color.class,  "RED"); SYSTEM.OUT.PRINTLN (color);//redsystem.out.println ("-------------------------");//values ()   Gets an enumerated array of the specified enumeration class Color[] colors = color.values ();for  (color c : colors)  { System.out.println (c);//red green blue}}}

0

-1

2

-------------------------

RED

GREEN

BLUE

-------------------------

0

1

2

-------------------------

RED

GREEN

BLUE

-------------------------

RED

-------------------------

RED

GREEN

BLUE


This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1878429

Enumeration of new Java features

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.