Java 简明教程
Java - Enum Constructor
Java enum是 class 的一种特殊类型,它表示一组预定义的常量值,可用于 switch expressions 进行比较,作为应用程序代码中的常量使用。
Java enum is a special kind of class that represents a group of pre-defined constant values and can be used in switch expressions for comparison, to be used as constants in application code.
默认情况下,一个 enum 不需要任何构造函数,其默认值与其声明相同。考虑以下示例:
By default, an enum does not require any constructor and its default value is the same as its declaration. Consider the following example:
enum WEEKDAY { MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY, SATURDAY, SUNDAY }
如果我们打印任何上述枚举的值,它将按照声明打印相同的字符串。
If we print the any of the value of above enum, it will print the same string as declared.
System.out.println(WEEKDAY.MONDAY);
它会按如下所示打印结果:
It will print the result as shown below:
MONDAY
Use of Enum Constructor
在我们需要为枚举分配默认值的情况下,我们可以创建下述枚举结构器:
Now in case, we want to assign a default value to enum, we can create a enum constructor as shown below:
enum WEEKDAY {
MONDAY("Day 1");
private final String description;
WEEKDAY(String description) {
this.description = description;
}
}
在这种情况下,我们使用结构器为枚举分配一个默认描述。
In this case, we’re assigning a default description to the enum using the constructor.
Scope of Enum Constructor
由于枚举是一个特殊的类,因此枚举结构器只能具有 private 或 package-private 修饰符。设置 public 标识符会导致枚举结构器出现编译时间错误。
As enum is a special class, an enum constructor can only have private or package-private modifier. Setting a public idenifier to enum constructor will attract a compile time error.
enum WEEKDAY {
MONDAY("Day 1");
private final String description;
// compiler error: illegal modifier, only private is permitted.
public WEEKDAY(String description) {
this.description = description;
}
}
以下是枚举结构器的有效声明
Following are valid declarations of enum constructor
Enum with Private Constructor
我们可以按照下述方式为枚举创建 private 结构器。
We can create a private constructor for an enum as shown below.
enum WEEKDAY {
MONDAY("Day 1");
private final String description;
// valid declaration
private WEEKDAY(String description) {
this.description = description;
}
}
Example
在这个例子中,我们使用 private 结构器创建了一个枚举 WEEKDAY。使用 private 结构器,我们使用其结构器设置枚举描述的值。
In this example, we’ve created an enum WEEKDAY with a private constructor. Using private constructor, we’re setting the value of enum description using its constructor.
package com.tutorialspoint;
// create the enum
enum WEEKDAY {
// create values of enum
MONDAY("Day 1"),
TUESDAY("Day 2"),
WEDNESDAY("Day 3"),
THRUSDAY("Day 4"),
FRIDAY("Day 5"),
SATURDAY("Day 6"),
SUNDAY("Day 7");
private final String description;
// private construtor to set default value
private WEEKDAY(String description) {
this.description = description;
}
// getter method to get the description
public String getDescription () {
return this.description;
}
}
public class Tester {
public static void main(String[] args) {
System.out.println(WEEKDAY.MONDAY.getDescription());
System.out.println(WEEKDAY.MONDAY);
}
}
让我们编译并运行上述程序,这将生成以下结果 −
Let us compile and run the above program, this will produce the following result −
Day 1
MONDAY
Enum with Package-private Constructor
我们可以按照下述方式为枚举创建 package-private 结构器。
We can create a package private constructor for an enum as shown below.
enum WEEKDAY {
MONDAY("Day 1");
private final String description;
// valid declaration
WEEKDAY(String description) {
this.description = description;
}
}
Example
在这个例子中,我们创建了一个枚举 WEEKDAY,因为它未传递任何修饰符,因此它是一个 package-private 结构器。使用这个结构器,我们使用其结构器设置枚举描述的值。
In this example, we’ve created an enum WEEKDAY with a package-private constructor as no modifier is passed. Using this constructor, we’re setting the value of enum description using its constructor.
package com.tutorialspoint;
// create the enum
enum WEEKDAY {
// create values of enum
MONDAY("Day 1"),
TUESDAY("Day 2"),
WEDNESDAY("Day 3"),
THRUSDAY("Day 4"),
FRIDAY("Day 5"),
SATURDAY("Day 6"),
SUNDAY("Day 7");
private final String description;
// private construtor to set default value
WEEKDAY(String description) {
this.description = description;
}
// getter method to get the description
public String getDescription () {
return this.description;
}
}
public class Tester {
public static void main(String[] args) {
System.out.println(WEEKDAY.MONDAY.getDescription());
System.out.println(WEEKDAY.MONDAY);
}
}
让我们编译并运行上述程序,这将生成以下结果 −
Let us compile and run the above program, this will produce the following result −
Day 1
MONDAY