Java 枚举和枚举的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19445183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between enum and Enumeration
提问by chetan mehta
Is there any difference between enum
datatype and Enumeration
Interface. I have become confused between the two.
enum
数据类型和Enumeration
接口之间有什么区别吗?我在这两者之间感到困惑。
I got my answer that they aren't related but that brings me to another question.
我得到了他们不相关的答案,但这让我想到了另一个问题。
We cannot instantiate interface . So what is the significance of this line
我们无法实例化 interface 。那么这条线有什么意义
Enumeration days = dayNames.elements();
Heres the complete code containing that line
这是包含该行的完整代码
import java.util.Vector;
import java.util.Enumeration;
public class EnumerationTester {
public static void main(String args[]) {
Enumeration days;
Vector dayNames = new Vector();
dayNames.add("Sunday");
dayNames.add("Monday");
dayNames.add("Tuesday");
dayNames.add("Wednesday");
dayNames.add("Thursday");
dayNames.add("Friday");
dayNames.add("Saturday");
days = dayNames.elements();
while (days.hasMoreElements()){
System.out.println(days.nextElement());
}
}
}
采纳答案by Sage
Enumeration is an interface: An object that implements the Enumeration
interface generates a series of elements, one at a time. Successive calls to the nextElement
method return successive elements of the series.
枚举是一个接口:实现该Enumeration
接口的对象一次生成一系列元素。对该nextElement
方法的连续调用将返回系列的连续元素。
For example, to print all elements of a Vector<E> v
:
例如,要打印 a 的所有元素Vector<E> v
:
for (Enumeration<E> e = v.elements(); e.hasMoreElements();)
System.out.println(e.nextElement());
enum is a data type: An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
枚举是一种数据类型:枚举类型是一种特殊的数据类型,它使变量成为一组预定义的常量。该变量必须等于为其预定义的值之一。
For example, you would specify a days-of-the-week enum
type as:
例如,您可以将星期几enum
类型指定为:
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
public static void main(String[] args)
{
System.out.ptintln(Day.SUNDAY); // print SUNDAY
}
Your Second Question:
你的第二个问题:
We cannot instantiate interface . So what is the significance of this line
我们无法实例化 interface 。那么这条线有什么意义
Enumeration days = dayNames.elements();
dayNames
is a Vector
, a collection just like List
. (There are differences, but that is beyond the scope of the question.). However, when daynames.elements()
is called, it returns an enumeration of the components of vector daynames
. The returned Enumeration object will generate all items added to this vector. The first item generated is the item at index 0
, then the item at index 1
, and so on.
dayNames
是一个Vector
,就像一个集合List
。(存在差异,但这超出了问题的范围。)。但是,当daynames.elements()
被调用时,它返回 vector 的组件的枚举daynames
。返回的 Enumeration 对象将生成添加到此向量的所有项目。生成的第一个项目是 at 的项目index 0
,然后是 at 的项目index 1
,依此类推。
回答by SpringLearner
as per the oracle docs
根据 oracle文档
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
枚举类型是一种特殊的数据类型,它使变量成为一组预定义的常量。该变量必须等于为其预定义的值之一。常见示例包括罗盘方向(北、南、东和西的值)和一周中的几天。
example of enum
枚举的例子
public class EnumTest {
Day day;
public EnumTest(Day day) {
this.day = day;
}
public void tellItLikeItIs() {
switch (day) {
case MONDAY:
System.out.println("Mondays are bad.");
break;
case FRIDAY:
System.out.println("Fridays are better.");
break;
case SATURDAY: case SUNDAY:
System.out.println("Weekends are best.");
break;
default:
System.out.println("Midweek days are so-so.");
break;
}
}
public static void main(String[] args) {
EnumTest firstDay = new EnumTest(Day.MONDAY);
firstDay.tellItLikeItIs();
EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
thirdDay.tellItLikeItIs();
EnumTest fifthDay = new EnumTest(Day.FRIDAY);
fifthDay.tellItLikeItIs();
EnumTest sixthDay = new EnumTest(Day.SATURDAY);
sixthDay.tellItLikeItIs();
EnumTest seventhDay = new EnumTest(Day.SUNDAY);
seventhDay.tellItLikeItIs();
}
}
The output is:
输出是:
Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.
Enumeration
枚举
The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.
Enumeration 接口定义了可以枚举(一次获取一个)对象集合中元素的方法。
example of enumeration
枚举示例
import java.util.Vector;
import java.util.Enumeration;
public class EnumerationTester {
public static void main(String args[]) {
Enumeration days;
Vector dayNames = new Vector();
dayNames.add("Sunday");
dayNames.add("Monday");
dayNames.add("Tuesday");
dayNames.add("Wednesday");
dayNames.add("Thursday");
dayNames.add("Friday");
dayNames.add("Saturday");
days = dayNames.elements();
while (days.hasMoreElements()){
System.out.println(days.nextElement());
}
}
}
output
输出
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
回答by Admit
To be short, Enumeration
is legacy Iterator
and Enum
is a data type.
简而言之,Enumeration
是遗留的Iterator
,Enum
是一种数据类型。
回答by Vaibhav Raj
Enumsare instance controlled classes in java. You can predefine the different flavors, a type can support. For ex.
枚举是 Java 中的实例控制类。您可以预定义不同的口味,一个类型可以支持。例如。
public enum Direction {
EAST, WEST, NORTH, SOUTH
}
defines a type named Directionwhich can be of 4 types. It's not permitted to instantiate these types from outside their type definition. You can check more on Enumhere.
定义了一个名为Direction的类型,它可以是 4 种类型。不允许从它们的类型定义之外实例化这些类型。您可以在此处查看有关Enum 的更多信息。
While Enumerationwas the old way to iterate through a collection. It has two methods nextElementand hasMoreElementswhich are more like nextand hasNextmethods of Iteratorinterface. In old APIs like Servletwe can still see some methods which return Enumeration. For ex.
虽然枚举是迭代集合的旧方法。它有两个方法nextElement和hasMoreElements,它们更像是Iterator接口的next和hasNext方法。在像Servlet这样的旧 API 中,我们仍然可以看到一些返回Enumeration 的方法。例如。
Java.util.Enumeration<String> paramNames = request.getParameterNames();