`
zjwlonger
  • 浏览: 15814 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java.lang.Eunm 类用法

    博客分类:
  • java
阅读更多

package enumeration;

 

public class TestEnum {

public enum Jobs { ENGINEER,PROGRAMMER,SALES};

public enum NewDepartment {ENGINEER, MARKETING, SALES, HR};

public static void main(String[] args)

{

 //compareTo

 int i = NewDepartment.MARKETING.compareTo(NewDepartment.SALES);

 int i2 = NewDepartment.MARKETING.compareTo(NewDepartment.ENGINEER);

 int i3 = NewDepartment.MARKETING.compareTo(NewDepartment.MARKETING);

 System.out.printf("NewDepartment.MARKETING compare to NewDepartment.SALES is %d\n", i);

 System.out.printf("NewDepartment.MARKETING compare to NewDepartment.ENGINEER is %d\n", i2);

 System.out.printf("NewDepartment.MARKETING compare to NewDepartment.MARKETING is %d\n", i3);

 //equals

 boolean b = NewDepartment.HR.equals(NewDepartment.SALES);

 boolean b2 = NewDepartment.HR.equals(NewDepartment.HR);

 System.out.printf("HR and NewDepartment.SALES are equal? %b\n", b);

 System.out.printf("HR and HR are equal? %b\n", b2);

 

 //getDeclaringClass

 Class c = NewDepartment.ENGINEER.getDeclaringClass();

 Class c2 = Jobs.ENGINEER.getDeclaringClass();

 System.out.printf("NewDepartment.ENGINEER\'s declaring class is %s\n", c.getName());

 System.out.printf("ENGINEER\'s declaring is %s\n", c2.getName());

 

 //name

 String s = NewDepartment.ENGINEER.name();

 String s2 = NewDepartment.ENGINEER.toString();

 System.out.printf("s and s2 are equals? %b\n", s.equals(s2));

 

 //ordinal

 int j = NewDepartment.ENGINEER.ordinal();

 int j2 = NewDepartment.HR.ordinal();

 System.out.printf("NewDepartment.ENGINEER\'s ordinal is %d\n", j);

 System.out.printf("NewDepartment.HR\'s ordinal is %d\n", j2);

 

 //valueOf

 NewDepartment dept = NewDepartment.valueOf("HR");

 boolean b3 = dept.equals(NewDepartment.HR);

 System.out.printf("dept(%s) is HR? %b\n", dept.name(), b3);

 

 //values

 NewDepartment[] depts = NewDepartment.values();

 System.out.printf("NewDepartment type has %d values, there are %s, %s, %s and %s.\n",

     depts.length, depts[0].name(), depts[1].name(), depts[2].name(), depts[3].name());

 

}

 

}

 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics