Java Basics Enumeration Magical

Source: Internet
Author: User

For the enumeration, the beginner of Java may have been in contact with us, but before graduation, in fact, never know how the enumeration is used in the real work, what is the use of enumerations? Next, the blogger introduced enumeration in the actual work of a use scenario, this article is only suitable for beginners to look at the small rookie, the big God did not laugh at this is hydrology oh, haha!

First, the use of the scene

In the actual work, such as I have a choice of family relations dropdown box, everyone will be a combination of value and DESC, generally we are saved to the database is value (English), and then the user sees Desc (Chinese). However, many of my pages will use such a drop-down box, and perhaps even a lot of systems will need to maintain the consistency of the data source, so you can save with an enumeration, and then provide an interface, all the front-end display of the data source is obtained from this interface.

May see this paragraph is still a bit of a mask, the following directly on the code, the combination of code should be clear.

Ii. Examples of Use

2.1. Set up the enumeration as follows

 PackageCom.luo.test; Public enumfamilyrelation {/** Parents * *PARENT ("PARENT","Parents"),/** Spouse * *Spouse ("Spouse","Spouse"),/** Brothers * *Brouther ("Brouther","Brother"),/** Sisters * *SISTER ("SISTER","Sisters"),/** Classmate * *Schoolmate ("Schoolmate","Classmate"),/** Friend * *FRIEND ("FRIEND","Friends");PrivateString value;PrivateString desc;Private familyrelation(string value, String desc) { This. SetValue (value); This. SETDESC (DESC); } PublicStringGetValue() {returnValue } Public void SetValue(String value) { This. value = value; } PublicStringGetDesc() {returnDesc } Public void Setdesc(String desc) { This. desc = desc; }}

2.2. Use of enumerations

The next thing we need to do is to get the value of the enumeration above and then save it to the list, and if you use SPRINGMVC, just pass the list to the front end, and then use JS in the front-end to put it in the Select node.

The explanation here is to put the enumeration in the list section.

Build a dto as follows:

Package com.luo.test; Public classFamilyrelationdto {//Relationship value    PrivateStringvalue;//Relationship Description    PrivateString desc; PublicStringGetValue() {return value; } Public void SetValue(Stringvalue) { This.value=value; } PublicStringGetDesc() {returnDesc } Public void Setdesc(String desc) { This. desc = desc; }}

The enumeration is traversed, and the values inside the enumeration are placed inside the list:

Packagecom. Luo. Test;Import Java. Util. ArrayList;Import Java. Util. List;public class Enumtest {public static void main (String args[]) {//Traversal enumeration System. out. println("----------Traversal enumeration-------------");for (Familyrelation familyrelation:familyrelation. Values()) {System. out. println(familyrelation. GetValue() +":"+ familyrelation. GetDesc());}//The enumeration is stored in the list list<familyrelationdto> list = new arraylist<familyrelationdto> ();for (Familyrelation familyrelation:familyrelation. Values()) {familyrelationdto familyrelationdto = new Familyrelationdto ();Familyrelationdto. SetValue(familyrelation. GetValue());Familyrelationdto. Setdesc(familyrelation. GetDesc());List. Add(familyrelationdto);} System. out. println("list length:"+ List. Size());}}

Finished, although the content is a little short, but it is not appropriate to join the other, so I wrote a separate blog. It's too water to blame, huh?

Java Basics Enumeration Magical

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.