Java 枚举

Java 枚举,枚举是一种特殊类型的数据类型,它基本上是常量的集合(集合)。在本教程中,我们将学习如何在 Java 中使用枚举以及我们可以使用它们的可能场景。

这就是我们定义Enum的方式

public enum Directions{
  EAST, 
  WEST, 
  NORTH, 
  SOUTH
}

这里我们有枚举类型的变量方向,它是四个常数EASTWESTNORTHSOUTH的集合。

如何为枚举类型赋值?

Directions dir = Directions.NORTH;

变量dir的类型为Directions(即枚举类型)。此变量可以取可能的四个值(EASTWESTNORTHSOUTH)中的任何值。在这种情况下,它设置为NORTH

if-else语句中使用Enum类型

这就是我们如何在if-else逻辑中使用枚举变量。

/* You can assign any value here out of
 * EAST, WEST, NORTH, SOUTH. Just for the
 * sake of example, I'm assigning to NORTH
 */
Directions dir = Directions.NORTH;  

if(dir == Directions.EAST) {
  // Do something. Write your logic
} else if(dir == Directions.WEST) {
     // Do something else
  } else if(dir == Directions.NORTH) {
     // Do something 
    } else {
        /* Do Something. Write logic for 
         * the remaining constant SOUTH
         */ 
      }

枚举示例

这只是演示使用枚举的一个示例。如果您了解核心部分和基础知识,您就可以根据需求编写自己的逻辑。

public enum Directions{
      EAST, 
      WEST, 
      NORTH, 
      SOUTH
}
public class EnumDemo
{
   public static void main(String args[]){
    Directions dir = Directions.NORTH;  
    if(dir == Directions.EAST) {
        System.out.println("Direction: East");
    } else if(dir == Directions.WEST) {
        System.out.println("Direction: West");
      } else if(dir == Directions.NORTH) {
          System.out.println("Direction: North");
        } else {
        System.out.println("Direction: South");
          }
   }
}

输出:

Direction: North

Switch-Case语句中使用Enum

下面是演示在switch-case语句中使用枚举的示例。

public enum Directions{
      EAST, 
      WEST, 
      NORTH, 
      SOUTH
}
public class EnumDemo
{
   Directions dir;
   public EnumDemo(Directions dir) {
      this.dir = dir;
   }
   public void getMyDirection() {
     switch (dir) {
       case EAST:
          System.out.println("In East Direction");
          break;

       case WEST:
          System.out.println("In West Direction");
          break;

       case NORTH: 
          System.out.println("In North Direction");
          break;

       default:
          System.out.println("In South Direction");
          break;
     }
   }

    public static void main(String[] args) {
        EnumDemo obj1 = new EnumDemo(Directions.EAST);
        obj1.getMyDirection();
        EnumDemo obj2 = new EnumDemo(Directions.SOUTH);
        obj2.getMyDirection();
    }
}

输出:

In East Direction
In South Direction

如何遍历Enum变量

class EnumDemo
{
    public static void main(String[] args) {
        for (Directions dir : Directions.values()) {
            System.out.println(dir);
        }
    }
}

此代码将显示所有四个常量。

枚举字段和方法

让我们先举一个例子然后我们将详细讨论它:

public enum Directions{
  EAST ("E"), 
  WEST ("W"), 
  NORTH ("N"), 
  SOUTH ("S")
  ; 
  /* Important Note: Must have semicolon at
   * the end when there is a enum field or method
   */
  private final String shortCode;

  Directions(String code) {
      this.shortCode = code;
  }

  public String getDirectionCode() {
      return this.shortCode;
  }
}
public class EnumDemo
{
    public static void main(String[] args) {
        Directions dir = Directions.SOUTH;
        System.out.println(dir.getDirectionCode());
        Directions dir2 = Directions.EAST;
        System.out.println(dir2.getDirectionCode());
    }
}

输出:

S
E

正如您在本示例中所看到的,我们为每个常量都有一个字段shortCode,以及一个方法getDirectionCode(),它基本上是该字段的获取器方法。当我们定义一个像EAST ("E")这样的常量时,它会使用传递的参数调用枚举构造函数(参见上例中的构造函数Directions)。这样,传递的值被设置为相应枚举常数EAST("E")的字段的值,它会调用构造函数Directions("E")this.shortCode = code,即this.shortCode = "E",常数EASTshortCode字段设置为"E"

注意事项:

1)在定义枚举时,应在任何字段或方法之前首先声明常量。

2)当在Enum中声明了字段和方法时,枚举常量列表必须以分号(;)结尾。

赞(0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址