Enumeration of Java Foundations

Source: Internet
Author: User

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

Workaround:

1) make a judgment in the Setgrade method and throw an exception if it does not conform to the format requirement

2) Restrict user's choice directly, and define user's input by custom class simulation enumeration

Write a grade class, private constructor, providing 5 static constants representing instances of a 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 ();

}

An enumeration class is a Java class, or you can declare properties, methods, constructors

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, or it 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;

}

Provides a method to return local information of an enumeration

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

Publicabstract String tolocalestring ();

}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.