Java 类中的枚举
java 8object oriented programmingprogramming
是的,我们可以在 Java 类中创建枚举。
在类中设置枚举。这里,我们有一个包含四个常量的枚举 −
enum Devices { LAPTOP, MOBILE, TABLET, DESKTOP; }
现在,在 main() 方法中创建对象 −
Devices d = Devices.MOBILE; System.out.println(d); d = Devices.DESKTOP; System.out.println(d);
以下是完整示例 −
示例
public class Demo { enum Devices { LAPTOP, MOBILE, TABLET, DESKTOP; } public static void main(String[] args) { Devices d = Devices.MOBILE; System.out.println(d); d = Devices.DESKTOP; System.out.println(d); d = Devices.TABLET; System.out.println(d); } }
输出
MOBILE DESKTOP TABLET