简体   繁体   中英

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". 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

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

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. 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 . 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. naming conventions: class names should be singular ( BuySection , not BuySections ), enums should begin with uppercase letters (as should all types, eg ItemsA , not 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM