简体   繁体   English

Java枚举泛型

[英]Java enum generics

I have a class as below, 我的课程如下

public class Baseclass {
   private final EmployeeEnum empEnum;
   public Baseclass(EmployeeEnum e) {
       this.empEnum = e;
    }
}

Now I want to make the Baseclass generic and make it accept Enums of a certain type. 现在,我想使Baseclass通用并使其接受某种类型的Enums。

Since Enum cant extend a class I have created an interface IEnum and made EmployeeEnum and ManagerEnum (the new enum I created) implement the interface. 由于枚举不能扩展类我已经创建了一个接口IEnum并提出EmployeeEnumManagerEnum (新枚举我创建)实现的接口。

I have made changes to Baseclass as below, 我对Baseclass做了如下更改,

public class Baseclass {
   private final Enum<?> empEnum;
   public Baseclass(Enum<?> e) {
       this.empEnum = e;
    }
}

Is there a better way to do this? 有一个更好的方法吗?

Cheers!! 干杯!!

If you merely want any enum then you can use E extends Enum<E> . 如果只需要任何枚举,则可以使用E extends Enum<E>

public class Baseclass<E extends Enum<E>> {

    private final E e;

    public Baseclass(E e) {
        this.e = e;
    }
}

All enum s extend Enum<E> so that they can inherit the standard methods such as name and values . 所有enum扩展Enum<E>以便它们可以继承标准方法,例如namevalues This is one of the reasons why enum s cannot extend other classes because no class in Java is allowed to extend two classes. 这是enum不能扩展其他类的原因之一,因为Java中不允许任何class扩展两个类。

Each sub-class must extend BaseClass with a specific enum like this: 每个子类都必须使用特定的枚举对BaseClass进行扩展,如下所示:

enum MyEnum {

    I, Me, My, Mine;
}

class A extends BaseClass<MyEnum> {

    public A(MyEnum e) {
        super(e);
    }

}

If you want further restrictions - such as making subclasses only use enums of a special type (such as implementing an interface) then you can add the interface to the generic like this: 如果您想要进一步的限制-例如使子类仅使用特殊类型的枚举(例如实现接口),则可以将接口添加到泛型中,如下所示:

public interface SpecialEnum {

}

enum MyEnum implements SpecialEnum {

    I, Me, My, Mine;
}

enum NotSpecialEnum {

    Am, I, Special;
}

public class BaseClass<E extends Enum<E> & SpecialEnum> {

    private final E e;

    public BaseClass(E e) {
        this.e = e;
    }
}

class A extends BaseClass<MyEnum> {

    public A(MyEnum e) {
        super(e);
    }

}

// This is not allowed.
class B extends BaseClass<NotSpecialEnum> {

    public A(NotSpecialEnum e) {
        super(e);
    }

}

You can even put the enum inside the extending class: 您甚至可以将枚举放入扩展类中:

class A extends BaseClass<A.AnotherEnum> {

    enum AnotherEnum implements SpecialEnum {

        Hello, All;
    }

    public A(AnotherEnum e) {
        super(e);
    }

}

Is there a better way to do this? 有一个更好的方法吗?

Better way to do what? 更好的方法是做什么? You could change to the code below which would limit you to enums that extend IEnum: 您可以更改为以下代码,该代码将限制您使用扩展IEnum的枚举:

class Baseclass<T extends Enum<T> & IEnum> {
    private final T empEnum;
    public Baseclass(T e) {
        empEnum = e;
    }
}

So T extends Enum<T> - T must be an Enum & - and IEnum - T must extend IEnum . 因此, T extends Enum<T> -T必须是Enum & -,而IEnum - T必须扩展IEnum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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