如果枚举是所需元素之一,请检查 setter
Check in the setter if an Enum is one of the desire element
我想验证传递给 setter 的参数。
我有一个枚举,在这个枚举中我有一种汽车颜色。
在 setter 中,我想检查传入的参数是否是我的 Color 枚举的一部分。
我正在寻找任何枚举方法,但我还没有找到。我尝试使用正则表达式,但这不是个好主意。
public enum Color {
BLUE, RED, BLACK, WHITE
}
public void setColor(String color) {
//hear should be a method checking if a color contain enum
this.color = color;
}
我希望这个元素将包含一个枚举。
tl;博士
In the setter I want to check if the passed in argument is a part of my Color enum.
错误的方法。您应该传递一个对象,您的枚举对象之一,而不是颜色名称的文本。
请参阅下面的 poster.setBackgroundColor( Color.GREEN )
示例。
这是在 Java 中使用枚举的要点:让编译器在编译时检查 是否传递了有效值,而不是你担心关于在 运行 时间对您的代码进行验证。
详情
看来您混淆了一些东西。您可能混淆了枚举实例(常量)、它的硬编码名称以及您可能希望在 运行 时间向用户显示的名称。
Enum
是一种方便地实例化 class 的预定数量实例的方法,每个实例都被分配了一个预定名称。例如,Month
, with Month.JANUARY
到 Month.DECEMBER
.
常量名称
如果您想要实例的硬编码名称,按照惯例全部大写(作为常量),则调用 toString
.
String constantName = Month.JANUARY.toString() ;
JANUARY
在这里非常清楚:带有文本“JANUARY”的String
对象是由名为JANUARY
的Month
对象生成的。 Month
对象和 String
对象是完全独立和不同的。 String
的文本只是 String
的值的表示=19=]对象。
如果要通过该硬编码实例名称获取常量,请调用 valueOf
。
Month m = Month.valueOf( "JANUARY" ) ; // Returns a `Month` object, not text.
尽量不要养成valueOf
的习惯。调用 valueOf
违背了 Java 中枚举的目的和功能。您应该传递枚举对象,而不是它们名称的字符串。
显示名称
这些全大写的英文值可能足以用于日志记录和调试。但是我们不太可能希望全大写的英文文本出现在我们的用户界面和报告中。因此,在编写自己的 Enum
时,添加一个名为 getDisplayName
, as seen on Month
& DayOfWeek
的方法。缩写长度指定TextStyle
,翻译中使用的人类语言和文化规范指定Locale
。
String output = m.getDisplayName(
TextStyle.FULL ,
Locale.CANADA_FRENCH
) ;
janvier
如何在您自己的枚举上实现它?那么,您的枚举实际上是 Enum
的子 class。所以你的枚举实际上是一个 Java class 和其他枚举一样,因为它可以有构造函数和方法。为每个声明的枚举实例名称添加参数。
示例class
这是单个 .java
文件中枚举 class 的完整示例,其中包含要演示的 main
方法。
package work.basil.example;
import java.util.EnumSet;
import java.util.Set;
public enum Color {
BLACK( "Black" ), // Pass arguments to each declared constant instant name, as you are really calling the constructor on each `static` `final` class constant object being automatically instantiated.
GREY( "Grey" ),
RED( "Red" ),
GREEN( "Green" ),
BLUE( "Blue" );
private String displayName; // Store the display name as a member variable on each enum instance.
// Constructor
Color ( String displayName ) { // Define a constructor taking the display name argument you passed above.
this.displayName = displayName;
}
String getDisplayName ( ) {
return this.displayName;
}
public static void main ( String[] args ) {
System.out.println( "Color.BLACK.toString(): " + Color.BLACK.toString() );
System.out.println( "Color.BLACK.getDisplayName(): " + Color.BLACK.getDisplayName() );
Set < Color > monochromeColors = EnumSet.of( Color.BLACK , Color.GREY );
boolean isRedMonochrome = monochromeColors.contains( Color.RED ); // ➙ false.
System.out.println( "isRedMonochrome: " + isRedMonochrome );
}
}
当运行.
Color.BLACK.toString(): BLACK
Color.BLACK.getDisplayName(): Black
isRedMonochrome: false
注意使用 EnumSet
(或 EnumMap
)来收集枚举实例。这里我们在一个名为 monochromeColors
的 Set
中为黑色和灰色定义了一个 Color
对象的集合,但忽略了红色、绿色和蓝色。
要使用您的枚举,想象一个 Poster
class 和一个 setter 方法作为海报的背景颜色。
Poster poster = new Poster() ;
poster.setBackgroundColor( Color.GREEN ) ;
无需文字 – 只需使用对象
请注意没有涉及文本。不需要实例名称的字符串,也不需要显示名称。使用枚举实例是关于使用对象,而不是文本,不是 String
。使用枚举对象可确保有效值,提供 type-safety,并使您的代码更加自文档化。
使用名称
让我们添加需要这些名称的代码。首先用于日志记录,在 Poster
.
上添加一个 toString
方法
@Override public String toString() {
return "Poster{ name=" + this.name + " | backgroundColor: " + this.backgroundColor.toString() + " }" ; // Shows "GREEN"
}
在用户界面中显示海报时,显示颜色的名称。
Label label = new Label(
"Background color: " +
poster.getBackgroundColor().getDisplayName() // Shows "Green".
) ;
我想验证传递给 setter 的参数。
我有一个枚举,在这个枚举中我有一种汽车颜色。
在 setter 中,我想检查传入的参数是否是我的 Color 枚举的一部分。
我正在寻找任何枚举方法,但我还没有找到。我尝试使用正则表达式,但这不是个好主意。
public enum Color {
BLUE, RED, BLACK, WHITE
}
public void setColor(String color) {
//hear should be a method checking if a color contain enum
this.color = color;
}
我希望这个元素将包含一个枚举。
tl;博士
In the setter I want to check if the passed in argument is a part of my Color enum.
错误的方法。您应该传递一个对象,您的枚举对象之一,而不是颜色名称的文本。
请参阅下面的 poster.setBackgroundColor( Color.GREEN )
示例。
这是在 Java 中使用枚举的要点:让编译器在编译时检查 是否传递了有效值,而不是你担心关于在 运行 时间对您的代码进行验证。
详情
看来您混淆了一些东西。您可能混淆了枚举实例(常量)、它的硬编码名称以及您可能希望在 运行 时间向用户显示的名称。
Enum
是一种方便地实例化 class 的预定数量实例的方法,每个实例都被分配了一个预定名称。例如,Month
, with Month.JANUARY
到 Month.DECEMBER
.
常量名称
如果您想要实例的硬编码名称,按照惯例全部大写(作为常量),则调用 toString
.
String constantName = Month.JANUARY.toString() ;
JANUARY
在这里非常清楚:带有文本“JANUARY”的String
对象是由名为JANUARY
的Month
对象生成的。 Month
对象和 String
对象是完全独立和不同的。 String
的文本只是 String
的值的表示=19=]对象。
如果要通过该硬编码实例名称获取常量,请调用 valueOf
。
Month m = Month.valueOf( "JANUARY" ) ; // Returns a `Month` object, not text.
尽量不要养成valueOf
的习惯。调用 valueOf
违背了 Java 中枚举的目的和功能。您应该传递枚举对象,而不是它们名称的字符串。
显示名称
这些全大写的英文值可能足以用于日志记录和调试。但是我们不太可能希望全大写的英文文本出现在我们的用户界面和报告中。因此,在编写自己的 Enum
时,添加一个名为 getDisplayName
, as seen on Month
& DayOfWeek
的方法。缩写长度指定TextStyle
,翻译中使用的人类语言和文化规范指定Locale
。
String output = m.getDisplayName(
TextStyle.FULL ,
Locale.CANADA_FRENCH
) ;
janvier
如何在您自己的枚举上实现它?那么,您的枚举实际上是 Enum
的子 class。所以你的枚举实际上是一个 Java class 和其他枚举一样,因为它可以有构造函数和方法。为每个声明的枚举实例名称添加参数。
示例class
这是单个 .java
文件中枚举 class 的完整示例,其中包含要演示的 main
方法。
package work.basil.example;
import java.util.EnumSet;
import java.util.Set;
public enum Color {
BLACK( "Black" ), // Pass arguments to each declared constant instant name, as you are really calling the constructor on each `static` `final` class constant object being automatically instantiated.
GREY( "Grey" ),
RED( "Red" ),
GREEN( "Green" ),
BLUE( "Blue" );
private String displayName; // Store the display name as a member variable on each enum instance.
// Constructor
Color ( String displayName ) { // Define a constructor taking the display name argument you passed above.
this.displayName = displayName;
}
String getDisplayName ( ) {
return this.displayName;
}
public static void main ( String[] args ) {
System.out.println( "Color.BLACK.toString(): " + Color.BLACK.toString() );
System.out.println( "Color.BLACK.getDisplayName(): " + Color.BLACK.getDisplayName() );
Set < Color > monochromeColors = EnumSet.of( Color.BLACK , Color.GREY );
boolean isRedMonochrome = monochromeColors.contains( Color.RED ); // ➙ false.
System.out.println( "isRedMonochrome: " + isRedMonochrome );
}
}
当运行.
Color.BLACK.toString(): BLACK
Color.BLACK.getDisplayName(): Black
isRedMonochrome: false
注意使用 EnumSet
(或 EnumMap
)来收集枚举实例。这里我们在一个名为 monochromeColors
的 Set
中为黑色和灰色定义了一个 Color
对象的集合,但忽略了红色、绿色和蓝色。
要使用您的枚举,想象一个 Poster
class 和一个 setter 方法作为海报的背景颜色。
Poster poster = new Poster() ;
poster.setBackgroundColor( Color.GREEN ) ;
无需文字 – 只需使用对象
请注意没有涉及文本。不需要实例名称的字符串,也不需要显示名称。使用枚举实例是关于使用对象,而不是文本,不是 String
。使用枚举对象可确保有效值,提供 type-safety,并使您的代码更加自文档化。
使用名称
让我们添加需要这些名称的代码。首先用于日志记录,在 Poster
.
toString
方法
@Override public String toString() {
return "Poster{ name=" + this.name + " | backgroundColor: " + this.backgroundColor.toString() + " }" ; // Shows "GREEN"
}
在用户界面中显示海报时,显示颜色的名称。
Label label = new Label(
"Background color: " +
poster.getBackgroundColor().getDisplayName() // Shows "Green".
) ;