Enumeration of Java Foundations

Source: Internet
Author: User

Problem: The value of an object's property cannot be arbitrary and must be one of the fixed set of values

The workaround:

1) Infer in the Setgrade method that throws an exception if it does not conform to the format requirement

2) directly limit the user's choice, define the user's input by defining the way of the class simulation enumeration.

Write a grade class. Private constructor, providing 5 static constants representing instances of the class

3) A new enumeration type is defined in Jdk5, specifically for solving such problems

4) enumeration is a special Java class that can define properties, methods, constructors, implement interfaces, inherit classes

Packagecn.itcast.enumeration;

Import Org.junit.Test;

Publicclass Demo1 {

@Test

public void Test () {

Print (grade.b);

}

public void print (Grade g)//A B C D E

{

String Value=g.getvalue ();

System.out.println (value);

}

}

/*

* 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 ();

*  }

*/

How to define constructors, methods, fields for enumerations

Enumgrade {//class A 100-90 B 89-80 C 79-70 D 69-60 E 59-0

A ("100-90"), B ("89-80"), C ("79-70"), D ("69-60"), E ("59-0");//Object

private String value;

Private Grade (String value) {

This.value=value;

}

Public String GetValue () {

return this.value;

}

}

Publicclass Demo1 {

@Test

public void Test () {

Print (grade.b); 89-80, Liang

}

public void print (Grade g)//A B C D E

{

String Value=g.getvalue ();

String Value2=g.localevalue ();

System.out.println (value+ "," +value2);

}

}

Enumerations with abstract methods

Enumgrade {//class A 100-90 excellent b89-80 good C 79-70 General D 69-60 difference E 59-0 fail

A ("100-90") {

Public String Localevalue () {

return "excellent";

}

},

B ("89-80") {

Public String Localevalue () {

return "good";

}

},

C ("79-70") {

Public String Localevalue () {

Return "General";

}

},

D ("69-60") {

Public String Localevalue () {

return "poor";

}

},

E ("59-0") {

Public String Localevalue () {

Return "Failed";

}

};//Object

private String value;

Private Grade (String value) {

This.value=value;

}

Public String GetValue () {

return this.value;

}

Public abstract String Localevalue ();

}

The enumeration class is a Java class. Properties can also be declared. Method, constructor

public enum GRADE4 {

A ("90-100"), B ("80-89"), C ("70-79"), D ("60-69"), E ("0-59");

Privatestring value;

PRIVATEGRADE4 (String value) {

This.value= value;

}

Publicstring GetValue () {

returnvalue;

}

}

An enumeration class is a Java class that can inherit abstractions and implement interfaces

public enum GRADE5 {

Abstract class cannot create instance object

A ("90-100") {

New a subclass instance of GRADE5

Publicstring tolocalestring () {

return "excellent";

}

}

, B ("80-89") {

New a subclass instance of GRADE5

Publicstring tolocalestring () {

return "good";

}

}

, C ("70-79") {

New a subclass instance of GRADE5

Publicstring tolocalestring () {

Return "Medium";

}

}

, D ("60-69") {

New a subclass instance of GRADE5

Publicstring tolocalestring () {

return "poor";

}

}

, E ("0-59") {

New a subclass instance of GRADE5

Publicstring tolocalestring () {

Return "Failed";

}

};

Privatestring value;

PRIVATEGRADE5 (String value) {

This.value= value;

}

Publicstring GetValue () {

returnvalue;

}

Provide a method to the outside. Returns the local information for the enumeration

A method does not know how to implement, can be defined as an abstract

Publicabstract String tolocalestring ();

}

Enumeration of Java Foundations

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.