[英]JAVA - Passing a generic enum as a method parameter for a constructor
I have three different types of enums (itemsA, itemsB and itemsC) and a Class "Product".我有三种不同类型的枚举(itemsA、itemsB 和 itemsC)和一个 Class“产品”。 So far, I'm passing one enum as a parameter for my product constructor, having three constructors:到目前为止,我将一个枚举作为我的产品构造函数的参数传递,具有三个构造函数:
static Product actualProduct;
public static void setActualProduct(itemsA currentProduct) {
actualProduct = new Product(currentProduct);
public static void setActualProduct(itemsB currentProduct) {
actualProduct = new Product(currentProduct);
public static void setActualProduct(itemsC currentProduct) {
actualProduct = new Product(currentProduct);
class Product class 产品
public class Product {
private String productId;
private String name;
public Product(itemsA item) {
this.productId = item.getId();
this.name = item.getProductName();
}
public Product(itemsB item) {
this.productId = item.getId();
this.name = item.getProductName();
this.category = item.getCategory();
}
public Product(itemsC item) {
this.productId = item.getId();
this.name = item.getProductName();
}
enum example: itemsA枚举示例:itemsA
public enum itemsA{
item1("00001", "itemName", BuySections.TOYS);
private final String id;
private final String productName;
private final BuySections section;
item1(String id, String name, BuySections section){
this.id = id;
this.productName = name;
this.section = section;
}
public String getProductName(){
return productName;
}
So, I think should be a way to implement this using some generics.所以,我认为应该是一种使用 generics 来实现这一点的方法。 Something like this:像这样的东西:
public static void setActualProduct(Enum<?> itemType) {
actualProduct = new Product<?>(itemType);
Any help would be very apreciated.任何帮助都会非常感激。
Yes, it is possible.对的,这是可能的。 you can make enum
implements a common interface
and create a constructor using the interface
.您可以使enum
实现一个通用interface
并使用该interface
创建一个构造函数。 following is the example以下是示例
interface GenericEnum{}
enum Item1 implements GenericEnum {
TOYS1;
}
enum Item2 implements GenericEnum {
TOYS2;
}
class Test {
public GenericEnum genericEnum;
public Test(GenericEnum genericEnum) {
this.genericEnum = genericEnum;
}
}
You can let your enums implement a common interface:你可以让你的枚举实现一个通用接口:
interface CommonInterface {
public String getId();
public String getProductName();
public BuySection getBuySection();
}
enum FstEnum implements CommonInterface {}
enum SndEnum implements CommonInterface {}
class Product<E extends Enum<E> & CommonInterface> {
E type;
Product(E type) {
this.type = type;
}
}
public static <E extends Enum<E> & CommonInterface> void setActualProduct(E itemType) {
actualProduct = new Product<E>(itemType);
}
A few remarks wrt.一些评论wrt。 naming conventions: class names should be singular ( BuySection
, not BuySections
), enums should begin with uppercase letters (as should all types, eg ItemsA
, not itemsA
)命名约定: class 名称应该是单数( BuySection
,而不是BuySections
),枚举应该以大写字母开头(所有类型,例如ItemsA
,而不是itemsA
)
I don't understand why you define different enums containing just single values, instead of defining one single enum containing different values.我不明白你为什么定义只包含单个值的不同枚举,而不是定义一个包含不同值的单个枚举。 Something like this:像这样的东西:
public enum Item {
item1("00001", "itemName", BuySections.TOYS),
item2("00002", "itemName2", BuySections.TOYS),
item3("00003", "itemName3", BuySections.TOYS);
private final String id;
private final String productName;
private final BuySections section;
Item(String id, String name, BuySections section){
this.id = id;
this.productName = name;
this.section = section;
}
public String getProductName(){
return productName;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.