gpt4 book ai didi

Java 泛型。 child 延伸 parent

转载 作者:行者123 更新时间:2023-12-02 09:05:15 26 4
gpt4 key购买 nike

你能帮我一个泛型吗?我有一个要求,我有一个 UI 表单,但根据类型,表单会完全更改。我为每种类型的表单创建了具有公共(public)字段的父 DTO 和子 DTO。使用 vaadin 进行验证。我该如何让它发挥作用。 childdto 上的绑定(bind)方法给出错误。

The type ChildlDTO does not define getTitle(capture#10-of ? extends ParentDTO) that is applicable here

The method writeBean(capture#10-of ? extends ParentDTO) in the type Binder is not applicable for the arguments (ParentDTO)

private ParentDTO dto= new ChildDTO();
private Binder<? extends ParentDTO> binder = new Binder<>(ParentDTO.class);

binder.forField(type).asRequired("Please select type")
.bind(ParentDTO::getType, ParentDTO::setType);

绑定(bind)和写入方法出现以下编译错误

    binder.forField(title).asRequired("Please select Title")
.bind(ChildDTO::getTitle, ChildDTO::setTitle);

binder.writeBean(control);

亲子类

public abstract class ParentDTO
public class ChildDTO extends ParentDTO {

瓦丁粘合剂

public class Binder<BEAN> implements Serializable {

绑定(bind)和写入方法

Binding<BEAN, TARGET> bind(ValueProvider<BEAN, TARGET> getter,
Setter<BEAN, TARGET> setter);
public void writeBean(BEAN bean) throws ValidationException {

还有

最佳答案

只需选择 Binder<ParentDTO> ,那么你也可以为其编写扩展类。

但是,您将无法执行此操作

binder.forField(title).asRequired("Please select Title")
.bind(ChildDTO::getTitle, ChildDTO::setTitle);

因为不能保证传递给它的是 ChildDTO .

如果您需要该方法,那么您可以执行类似的操作,并为每种类型的 DTO 创建一个函数:

public Binder<ChildDTO> createChildBinder(ChildDTO bean) {
Binder<ChildDTO> binder = createBinder(bean);
TextField titleField = new TextField();
add(titleField);
binder.forField(titleField).asRequired()
.bind(ChildDTO::getTitle, ChildDTO::setTitle);
binder.readBean(bean);
return binder;
}

public Binder<ChildTwoDTO> createChildBinder(ChildTwoDTO bean) {
Binder<ChildTwoDTO> binder = createBinder(bean);
TextField languageField = new TextField();
add(languageField);
binder.forField(languageField).asRequired()
.bind(ChildTwoDTO::getLanguage, ChildTwoDTO::setLanguage);
binder.readBean(bean);
return binder;
}

public <T extends ParentDTO> Binder<T> createBinder(T bean) {
Binder<T> binder = new Binder<>();
binder.forField(typeField).asRequired("Better fill this...")
.bind(ParentDTO::getType, ParentDTO::setType);
return binder;
}

Full code

关于Java 泛型。 child 延伸 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59861335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com