Java code snippets _ 7

Source: Internet
Author: User

Package javaee. china. cxp;

Import org. junit. Test;

/**
* Function of enumeration: Some programs require a certain range of values instead of any data during runtime. JDK 5 used a custom class to solve this problem,
* After JDK 5, enumeration can be used directly.
* The New enum keyword in jdk5 is used to define an enumeration class.
* An enumeration can also contain constructors, fields, and methods.
*/
Public class Demo_7_emur {
 
@ Test
Public void test_6 (){
Print (Grade. );
Print2 (Grade2.A );
}
 
Public void print (Grade G ){
 
}
 
Public void print2 (Grade2G ){
 
}
 
/**
* The following method is replaced by the emur enumeration.
*/
Enum Grade2 {
 
A, B, C, D, E;
 
}
 
 
}

/**
* Jdk5 used custom classes to solve the problem.
* Privatize the Grade Constructor
* Provides a common static constructor.
*/
Class Grade {
 
Private Grade (){}
 
Public static final Grade A = new Grade ();
 
Public static final Grade B = new Grade ();
 
Public static final Grade C = new Grade ();
 
Public static final Grade D = new Grade ();
 
Public static final Grade E = new Grade ();
 
}

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.