Java: How to write a universal function that accepts Enum constants that implement this interface?

So, I have a list of enums that extend the interface:

public interface MyInterface {}

Then I have a few enumerations that extend the interface:

public enum A implements MyInterface {}

public enum B implements MyInterface {}

I need a function that will only accept an enumeration extending this interface. I cant:

public void MyFunction(MyInterface input)

because inside the function I create EnumSet using EnumSet.of (input). I cannod do

public <T extends Enum<T>> void myFunction(T input)

because inside the function I need to create a map that needs to be passed to another function. So is there any safe type for this without casting?

Edit: Fixed interface definitions.

+4
source share
1

:

public <T extends Enum<T> & MyInterface> void myFunction(T input)

. . void . . ! , , Java. .

+13

Source: https://habr.com/ru/post/1526385/


All Articles