对比 Java 中的枚举值
要比较枚举值,请使用 equals() 方法。
我们的 Devices 枚举有一些具有分配给它们的值得对象。
Devices d1, d2, d3; d1 = Devices.LAPTOP; d2 = Devices.LAPTOP; d3 = Devices.TABLET;
让我们比较它们 −
if(d3.equals(Devices.TABLET)) System.out.println("Devices are same."); else System.out.println("Devices are different.");
下面是一个示例 −
示例
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("Devices are same."); else System.out.println("Devices are different."); if(d3.equals(Devices.TABLET)) System.out.println("Devices are same."); else System.out.println("Devices are different."); } }
输出
Devices are same. Devices are same.
广告