在 Java 中实现 Switch on Enum
Java 中的 Enum 包含一组固定的常量。它们可以有字段、构造函数和方法。它增强了 Java 中的类型安全性。
以下示例在 Java 中对 Enumeration 实现了 Switch 语句 -
示例
public class Demo {
public static void main(String[] args) {
Laptop l = Laptop.Inspiron;
switch(l){
case Inspiron:
System.out.println("Laptop for home and office use!");
break;
case XPS:
System.out.println("Laptop for the ultimate experience!");
break;
case Alienware:
System.out.println("Laptop for high-performance gaming");
break;
}
}
}
enum Laptop {
Inspiron, XPS, Alienware;
}输出
Laptop for home and office use!
Advertisement
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP