混淆 枚举_与枚举混淆这里是一篇清楚的文章

本文翻译自Medium,旨在清晰解释枚举(Enum)的概念,帮助读者消除关于枚举的困惑。
摘要由CSDN通过智能技术生成

混淆 枚举

让我们从枚举开始……(Let’s begin with enum …)

The following are important points about enum :

以下是有关枚举的要点:

  • An enum is a group of predefined named constant in any programming language.

    枚举是任何编程语言中的一组预定义的命名常量

  • Enum is used when we know all possible values of the group before compiling the code.

    当我们在编译代码之前知道该组的所有可能值时,将使用Enum。
  • Example: Direction can be an enum as we know all possible values of direction that can be NORTH, SOUTH, EAST, and WEST and declared using the enum keyword.

    示例: Direction可以是一个枚举,因为我们知道了Direction的所有可能值,可以是NORTH,SOUTH,EASTWEST,并使用 枚举关键字。

  • Over the course of development, we may add a few new enum constant.

    在开发过程中,我们可能会添加一些新的枚举常量

  • Enums are very powerful as they may have instance variables, instance methods, and constructors.

    枚举非常强大,因为它们可能具有实例变量,实例方法构造函数。

  • Each enum constant should be written in capital letters.

    每个枚举常量都应以大写字母表示。

  • Every enum constant is by default internally public static final of type Enum declared.

    默认情况下,每个枚举常量都是声明为Enum类型的内部公共静态最终形式。

我们来看一个枚举的例子: (Let’s see an example of enum :)

Enum example outside class 课堂外的枚举示例

输出:(OUTPUT :)

Direction name : NORTHDirection name : SOUTHDirection name : WESTDirection name : EAST

NOTE : Enum can be declared outside a class or inside a class but cannot be declared inside a method .

注意:枚举可以在类外部或类内部声明,但不能在方法内部声明。

You can see that Direction is the name of an enum that has four constants. Each constant is a public static final reference that points to enum Object.

您可以看到Direction是具有四个常量的枚举的名称。 每个常量都是一个公共静态变量 指向枚举Object的最终引用。

public static final Direction NORTH = new NORTH();

Here NORTH is a reference variable to Direction object which is the public static final of type Direction. As it is public static, hence it can be accessed via enum name from anywhere .Eg. Direction.NORTH.You can see that object is created using a new keyword and then the constructor is called and the object reference is assigned to constant.

在这里NORTHDirection对象的引用变量, Direction对象是Direction类型的公共静态最终形式。 由于它是公共静态的,因此可以从任何地方通过枚举名称访问它。 方向。 您可以看到使用关键字创建了对象,然后调用了构造函数,并将对象引用分配给了常量。

NOTE : In Enum , constants must be declared first and then instance variables , constructors and methods .

注意:在Enum中,必须先声明常量,然后再声明实例变量,构造函数和方法。

在切换情况下如何使用枚举常量作为参数? (How to use enum constant as an argument in switch case?)

Below I’ve written a program which contains

下面我写了一个程序,其中包含

  1. Enum constants

    枚举常量

  2. Instance variable

    实例变量

  3. Constructor

    建设者

  4. Instance methods

    实例方法

  5. Switch case in the Main class.

    在Main类中切换大小写

枚举方向: (Enum Direction:)

Enum example with an instance variable, methods, and constructor 带有实例变量,方法和构造函数的枚举示例

Main作为开关示例的Driver(Main as Driver class for switch example :)

输出: (OUTPUT:)

Direction name : NORTH
Direction name : SOUTH
Direction name : WEST
Direction name : EAST
East case matched!

枚举和类之间有什么区别? (What Are the Differences Between Enum and Class?)

The following are important points to note :

以下是要注意的重要事项:

  1. Every enum is internally implemented as a class.

    每个枚举在内部都作为一个类实现。
  2. No extends clause allowed for enum as every enum internally extends java.lang.Enum class.

    由于每个枚举都在内部扩展java.lang.Enum类,因此没有enum的extends子句。

  3. One enum cannot extend another enum but one class can extend another class.

    一个枚举不能扩展另一个枚举,但是一个类可以扩展另一类。
  4. Enum objects cannot be created outside the enum or in other classes using a new keyword, but a class object can be created using a new keyword in the same or other classes/packages.

    枚举对象不能使用关键字在枚举之外或在其他类中创建,但是可以在相同或其他类/包中使用关键字创建类对象。

让我们谈谈枚举和继承: (Let’s Talk About Enum and Inheritance:)

  1. All enum extends java.lang.Enum class implicitly and one class/enum can only extend one class at a time, hence an enum cannot extend another enum/class

    所有枚举都隐式扩展了java.lang.Enum类,并且一个类/枚举一次只能扩展一个类,因此一个枚举不能扩展另一个枚举/类

  2. toString() method is overridden in java.lang.Enum and return enum name.

    toString()方法在java.lang.Enum中被重写并返回枚举名称。
  3. Enum can implement any number of interfaces

    枚举可以实现任意数量的接口

现在,让我们看一些重要的枚举方法:(Now Let’s See Some of the Important Enum Methods:)

1. values()返回所有枚举对象的数组。 (1. values() returns an array of all enum objects.)

for (Direction direction : Direction.values()) {
System.out.println("Direction name : " + direction);
}

输出: (OUTPUT:)

Direction name : NORTH
Direction name : SOUTH
Direction name : WEST
Direction name : EAST

2. ordinal()返回枚举常量的索引。 (2. ordinal() returns index of enum constant .)

System.out.println("Index of EAST is : "+Direction.EAST.ordinal());

输出: (OUTPUT :)

Index of EAST is : 3

3. valueOf()返回此方法中指定为参数(字符串参数)的枚举常量对象。如果方法中指定的枚举不存在,则发生IllegalArgumentException。 (3. valueOf() returns enum constant object which is specified in this method as a parameter (String parameter).If the specified enum in the method does not exist then IllegalArgumentException occurs.)

System.out.println("EAST enum constant is : "+Direction.valueOf("EAST"));

输出: (OUTPUT :)

EAST enum constant is : EAST

The below code throws an illegal argument exception at runtime as “NORTH-EAST” is not an enum constant.

以下代码在运行时会抛出非法的参数异常,因为“ NORTH-EAST”不是枚举常量。

System.out.println("EAST enum constant is : "+Direction.valueOf("NORTH-EAST"));
--------------------------------------------------------------------Exception in thread “main” java.lang.IllegalArgumentException: No enum constant com.vikram.Direction.NORTH-EAST
at java.lang.Enum.valueOf(Enum.java:238)
at com.vikram.Direction.valueOf(Main.java:7)
at com.vikram.Main.main(Main.java:85)

Let’s stop here for this article. Hope I have cleared all aspects of enum concepts and usages in java.

让我们在这里停止。 希望我已经清除了Java中枚举概念和用法的所有方面。

Found this article useful? You can Follow me for more such useful articles and keep learning …

觉得这篇文章有用吗? 您可以关注我以获取更多此类有用的文章,并继续学习……

Other useful articles are listed below :

下面列出了其他有用的文章:

翻译自: https://medium.com/javarevisited/confused-with-enum-here-is-an-article-to-get-it-clear-ad56c034e97e

混淆 枚举

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值