Java 在switch语句中使用Enum

Java 在switch语句中使用Enum

枚举关键字

Java有一种特殊的数据类型,叫做Enum,它通常是一个常量的集合(set)。更准确地说,Java Enum类型是Java类的一种特殊形式。一个常量、一个过程等都可以包含在一个Enum中。可以在if语句、switch语句、迭代等情况下使用Enum关键字。

  • 默认情况下,枚举常量是公共的、静态的和最终的。
  • 使用点语法,枚举常量可以被访问。
  • 除了常量之外,枚举类还可以包含属性和方法。
  • 枚举类不能继承其他类,也不能创建它们的对象。
  • 枚举类只限于接口实现。

文件名:EnumExample.jav

// A Java program that 
// demonstrates how Enum 
// Keywords function when 
// specified outside of classes
enum Months {
    JAN,
    FEB,
    MAR,
    APR,
    MAY,
    JUN,
    JUL;
    AUG;
    SEP;
   OCT;
   NOV;
  DEC;
} 
public class EnumExample {
    // Main method
    public static void main(String args[])
    {
        Months m = Months.MAY;
        System.out.println(m);
    }
}

输出

MAY

switch关键字

当用户有很多选择,并希望为每个决定完成一个单独的任务时,Switch语句很有用。Switch语句使得比较一个变量的值和一个潜在值的列表成为可能。每个值都有一个不同的情况。通过break语句,switch Case语句经常被使用,尽管它不是必须的。

文件名:SwitchExample.java

// Java program to 
// demonstrate the use 
// of the switch statement
public class SwitchExample {
    public static void main(String args[])
    {
        // Declaring the variable for the case statements of switch
        int n = 5; 
        // Switch keyword
        switch (n) {
        // Case statements
        case 1:
            System.out.println(" The number is 1 ");
            break;
        case 2:
            System.out.println(" The number is 2 ");
            break;
        case 3:
            System.out.println(" The number is 3 ");
            break;
        // Last case is the default
        default:
            System.out.println(" The number is other than 1, 2 or 3");
        }
    }
}

输出

The number is other than 1, 2 or 3

enum关键字也与Switch语句兼容。Enum的使用方式类似于Java Switch case语句中的int primitive。下面的例子展示了Enum与类似Switch语句的功能。

例子1 :

当一个枚举在主类之外被使用时,会使用一个Switch语句。

文件名:EnumSwitch.java

// A Java program that demonstrates
// how the Enum keyword and 
// the Switch statement function
// Outside of the main class,
// enum keyword declared
enum Bikes {
    Honda,
    Pulsar,
    Passion,
    Yamaha,
    Apache,
    Suzuki;
}
// Main class
public class EnumSwitch {
    public static void main(String args[])
    {
        // Declaring the Enum variable
        Bikes b;
        b = Bikes.Apache;
        // using the Switch keyword
        switch (b) {
        // Case statements
        case Apache:
            System.out.println(" Hurray ! You have chosen Apache !");
            break;
        case Honda:
            System.out.println(" Hurray ! You have chosen Honda !");
            break;
        case Pulsar:
            System.out.println(" Hurray ! You have chosen Pulsar !");
            break;
        case Passion:
            System.out.println(" Hurray ! You have chosen Passion !");
            break;
        case Yamaha:
            System.out.println(" Hurray ! You have chosen Yamaha !");
            break;
        case Suzuki:
            System.out.println(" Hurray ! You have chosen Suzuki !");

        default:
            System.out.println(" Oops ! Sorry not in the list. ");
            break;
        }
    }
}

输出

Hurray ! You have chosen Apache!

前面提到的例子演示了当Enum被指定在主类之外时,Enum关键字和Switch案例指令是如何发挥作用的。

例2: 当使用Enum与Switch语句时,确保Enum在主类中。

文件名:EnumSwitch1.java

public class EnumSwitch1{
  // inside of the main class,
// enum keyword declared
  enum Bikes {
    Honda,
    Pulsar,
    Passion,
    Yamaha,
    Apache,
    Suzuki;
   }
    public static void main(String args[])
    {
        // Declaring the Enum variable
        Bikes b;
        b = Bikes.Apache;
        // using the Switch keyword
        switch (b) {
        // Case statements
        case Apache:
            System.out.println(" Hurray ! You have chosen Apache !");
            break;
        case Honda:
            System.out.println(" Hurray ! You have chosen Honda !");
            break;
        case Pulsar:
            System.out.println(" Hurray ! You have chosen Pulsar !");
            break;
        case Passion:
            System.out.println(" Hurray ! You have chosen Passion !");
            break;
        case Yamaha:
            System.out.println(" Hurray ! You have chosen Yamaha !");
            break;
        case Suzuki:
            System.out.println(" Hurray ! You have chosen Suzuki !");
        default:
            System.out.println(" Oops ! Sorry not in the list. ");
            break;
        }
    }
}

输出

Hurray ! You have chosen Apache!

前面提到的插图显示了,如果Enum是在主类内部声明的,Enum关键字如何与Switch case语句结合使用。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程