Java enum, javaenum

Source: Internet
Author: User

Java enum, javaenum

 

1/** 2 * Created by Edward on 2016/6/30. 3 */4 public class TestEnum {5 6 // Method 7 public enum SexEnum {8 MALE, FEMALE; 9} 10 11 // method 2 12 public enum SexEnum1 {13 MALE (1), 14 FEMALE (2); 15 16 private int id; 17 18 // private constructor 19 private SexEnum1 (int id) 20 {21 this. id = id; 22} 23 24 // enum object 25 public static SexEnum1 getEnum (int id) 26 {27 for (SexEnum1 s: SexEnum1.values ()) 28 {29 if (s. id = id) 30 return s; 31} 32 return null; 33} 34} 35 36 37 public static void main (String [] argv) 38 {39 // list the enum name40 for (SexEnum sex: SexEnum. values () 41 System. out. println (sex. name (); 42 43 // access 44 SexEnum sex = SexEnum through ENUM. MALE; 45 switch (sex) 46 {47 case MALE: 48 System. out. println ("male"); 49 break; 50 case FEMALE: 51 System. out. println ("female"); 52 break; 53 default: 54 System. out. println ("other"); 55 break; 56} 57 58 // get the enum object 59 SexEnum1 s = SexEnum1.getEnum (3); 60 switch (s) 61 {62 case MALE: 63 System. out. println ("male"); 64 break; 65 case FEMALE: 66 System. out. println ("female"); 67 break; 68 default: 69 System. out. println ("other"); 70 break; 71} 72 73} 74}

 

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.