Вопрос по java, generics – Как я могу требовать, чтобы универсальный параметр был перечислением, реализующим интерфейс?

44

Error: User Rate Limit Exceeded

class MyWidget <T extends Enum<T> > {
  MyWidget(Map<T, Integer> valueMap) {
    mValueMap = valueMap;
  }

  Map<T, Integer> mValueMap;
}

Error: User Rate Limit ExceededMyWidgetError: User Rate Limit ExceededmValueMapError: User Rate Limit ExceededEnumError: User Rate Limit ExceededInteger.

Error: User Rate Limit Exceeded

interface MyInterface {
  public Integer getValue();
}

enum MyEnum implements MyInterface {
  foo, bar;
  public Integer getValue() {
    return ordinal();
  }
}

Error: User Rate Limit ExceededMyWidgetError: User Rate Limit Exceeded

public class MyWidget<T extends Enum<T> extends MyInterface> {
  ...
}

Error: User Rate Limit ExceededgetValue()Error: User Rate Limit ExceededMyInterfaceError: User Rate Limit ExceededTError: User Rate Limit ExceededMyWidgetError: User Rate Limit Exceeded<T extends Enum<T> extends MyInterface>Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededMyWidget<T extends MyInterface>Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Ваш Ответ

2   ответа
1

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
82

&

public class MyWidget<T extends Enum<T> & MyInterface> {
    ...
}

extends

public class MyWidget<T extends Enum<T> & MyInterface & Serializable & Cloneable> {
    ...
}

[Note: this code sample should not be construed as an endorsement of the Cloneable interface; it was merely handy at the time.]

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededcode smellsError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededdoSomething(Enum<? extends SomeInterface>)

Похожие вопросы