java java.lang.enum_java.lang.Enum类方法

Example5.java

public class Example5

{

enum Jobs { ENGINEER,PROGRAMMER,SALES};

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());

}

}

执行结果如下所示:

NewDepartment.MARKETING compare to NewDepartment.SALES is

-1

NewDepartment.MARKETING compare to NewDepartment.ENGINEER is

1

NewDepartment.MARKETING compare to NewDepartment.MARKETING is

0

HR and NewDepartment.SALES are equal? false

HR and HR are equal? true

NewDepartment.ENGINEER's declaring class is

Example5$NewDepartment

ENGINEER's declaring is Example5$Jobs

s and s2 are equals? true

NewDepartment.ENGINEER's ordinal is 0

NewDepartment.HR's ordinal is 3

dept(HR) is HR? true

NewDepartment type has 4 values, there are ENGINEER, MARKETING,

SALES and HR.

下面介绍上面程序中所涉及的方法:

compareTo:比较两个枚举类型值的顺序。

equals:比较两个枚举类型值是否相等。

getDeclaringClass:取得该枚举类型的声明类。此方法跟Object.getClass()这个方法有点类似,但两者可能得到的结果不同,因为枚举类型类跟一般的类不太一样,它是属于常量且不能实例化,所以使用Object.getClass()方法,在概念上是比较奇怪。

name:取得该枚举类型值的名称字符串。这个方法跟toString有点类似,在没有修改的情况下,两者的返回值是一样的。习惯上的分法是,name方法返回声明时所对应的字符串;toString方法可以美化一下所要返回的字符串,让它看起来更friendly,不过你必须改写toString方法才行。

ordinal:取得该枚举类型的次序。这个号码由0开始编号,以程序7来说ENGINEERING为0、MARKETING为1、SALES为2,而HR为3。

toString:将枚举类型的值,以字符串的类型表示。你可根据程序的需要,自行改写默认的toString方法,让返回的字符串更具信息性。

valueOf:这个方法恰好跟name相反,你可以输入一个字符串,它会返回给你对应到的枚举类型的值。例如NewDepartment.valueOf("HR")会返回NewDepartment.HR。若你所传入的字符串在该枚举类型中,无法找到相对应的值,则会丢出java.lang.IllegalArgumentException异常。这里所传入的字符串是区分大小写的,所以当初在声明枚举类型的值时,也要注意到大小写的问题。

values:取出此枚举类型全部的值。

你可能感兴趣的:(java,java.lang.enum)