私は次のジェネリッククラスを持っています:
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());
ただし、これを実現するために、ジェネリック クラスにコンストラクターを含めたいと思います。何か案は?