Java通用类型的灵活性

时间:2017-03-21 04:09:02

标签: java generics enums

所以,假设我有一个枚举,"数据"。

public enum Data {

  FIRSTNAME(String.class, "John");

  private final Class<?> defaultClass;
  private final Object defaultData;

  Data(Class<?> clazz, Object data) {
    this.defaultClass = clazz;
    this.defaultData = data;
  }

  public Class<?> getDataClass() {
    return this.defaultClass;
  }
}

是否可以根据传递的Data枚举的getDataClass()响应创建一个获取其返回类型的方法?就是这样:

//This code obviously won't work, it's just another way of showing this.
public [data.getDataClass()] getData(Data data) {
   //Return the data.
}

0 个答案:

没有答案