Simple application of enum enum

Source: Internet
Author: User
Package com.ibm.enums;
	public class Testenum {public enum colorenums{red, green, yellow,blue;
		
		public enum male{Formale ("Girl", 1), Male ("Boy", 2);
		private String name;
		
	    private int index;
		Public String GetName () {return name;
		public void SetName (String name) {this.name = name;
		public int GetIndex () {return index;
		public void Setindex (int index) {this.index = index;
	    	////Construct method Private Male (String Name,int index) {this.name = name;
	    This.index = index; } public static void Main (string[] args) {//TODO auto-generated Method Stub//Traversal enumeration for (Colorenums CE:C
		Olorenums.values ()) {//The values here are the variables themselves System.out.println (CE);
		The value in the enumeration is obtained by "enumeration name. Value" when assigned colorenums a = Colorenums.green;
			Switch (a) {case red:System.out.println (' is red ');
		Break
			Case Green:System.out.println ("It is green");
		Break Case Yellow:System.out.println (' This is yellow ");
			
		Break
		
		///computes the number of enumerations System.out.println ("Colorenums Enumerator:" + colorenums.values (). length); Evaluates the index System.out.println (ColorEnums.blue.ordinal ()) of the enumeration; The Compare Method System.out.println (ColorEnums.blue.co) is implemented in the Blue location is 3, the It looks like Bigan from 0//enumeration method Mpareto (Colorenums.green)); 2//More complex enumeration Male for (Male m:male.values ()) {System.out.println (M.index + ":" + m.name);
		M.values () is an object System.out.println ();
 }

	}

}


Red
Green
Yellow
Blue
This is green
Colorenums Number of enumerations: 4
3
2
1:girl
2
2:boy
2


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.