我有以下通用类:
public class DropdownItem<V, D> {
private V value;
private D display;
public DropdownItem(V value, D display) {
this.value = value;
this.display = display;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public D getDisplay() {
return display;
}
public void setDisplay(D display) {
this.display = display;
}
}
如何为特定类型创建构造函数?
例如,
public DropdownItem(CustomClass custom) {
this(custom.getFoo(), custom.getBar());
}
或者
public DropdownItem(CustomClass custom) {
this.value = custom.getFoo();
this.display = custom.getBar();
}
这些解决方案都不起作用。在实现泛型类时它确实可以做到这一点:
DropdownItem<Integer, String> myItem = new DropdownItem<Integer, String>(custom.getFoo(), custom.getBar());
但是,我想在泛型类中包含一个构造函数来完成此操作。有任何想法吗?