Java中的枚举方法
让我们了解Java中的一些方法。
枚举ordinal()方法
使用Java中的ordinal()方法获取Java中枚举常量的序数。
以下是一个例子-
示例
public class Demo {
enum Devices {
LAPTOP, MOBILE, TABLET, DESKTOP;
}
public static void main(String[] args) {
Devices d1, d2, d3, d4;
d1 = Devices.LAPTOP;
d2 = Devices.LAPTOP;
d3 = Devices.TABLET;
d4 = Devices.DESKTOP;
System.out.println("Ordinal Values...");
for(Devices d : Devices.values()) {
System.out.print(d+" = ");
System.out.println(d.ordinal());
}
}
}输出结果
Ordinal Values... LAPTOP = 0 MOBILE = 1 TABLET = 2 DESKTOP = 3
枚举equals()方法
如果指定对象等于此枚举常量,则返回true。
让我们看一个例子-
示例
public class Demo {
enum Devices {
LAPTOP, MOBILE, TABLET;
}
public static void main(String[] args) {
Devices d1, d2, d3;
d1 = Devices.LAPTOP;
d2 = Devices.LAPTOP;
d3 = Devices.TABLET;
if(d1.equals(d2))
System.out.println("设备是相同的。");
else
System.out.println("设备不同。");
}
}输出结果
设备是相同的。
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短