我试图了解如何为非永久对象正确实现LeafValueEditor。这两种方式中的哪一种是正确的,还是应该使用其他方式?
public class Address {
public String line1;
public String city;
public String zip;
}
选项1:
public class AddressEditor implements LeafValueEditor<Address>
{
private String line1;
private String city;
private String zip;
private Address address;
public void setValue(Address value)
{
this.line1 = value.line1;
this.city = value.city;
this.zip = value.zip;
this.address = value;
}
public Address getValue()
{
this.address.line1 = this.line1;
this.address.city = this.city;
this.address.zip = this.zip;
return this.address;
}
}
选项2:
public class AddressEditor implements LeafValueEditor<Address>
{
private String line1;
private String city;
private String zip;
public void setValue(Address value)
{
this.line1 = value.line1;
this.city = value.city;
this.zip = value.zip;
}
public Address getValue()
{
Address a = new Address();
this.a.line1 = this.line1;
this.a.city = this.city;
this.a.zip = this.zip;
return a;
}
}
答案 0 :(得分:1)
可能两者都没有,尽管两者在技术上都可行。
LeafValueEditor是叶值的编辑器 - 即通常不包含其他值的值。通常,页面上可见的文本或日期或数字字段是叶子编辑器,这些叶子节点包含在普通的编辑器中。
在这种情况下,它看起来像这样:
public class AddressEditor extends Composite implements Editor<Address> {
// not private, fields must be visible for the driver to manipulate them
// automatically, could be package-protected, protected, or public
protected TextBox line1;//automatically maps to getLine1()/setLine1(String)
protected TextBox city;
protected TextBox zip;
public AddressEditor() {
//TODO build the fields, attach them to some parent, and
// initWidget with them
}
}
请参阅http://www.gwtproject.org/doc/latest/DevGuideUiEditors.html#Editor_contract,了解更多详细信息,了解如何通过这么少的布线自动将它们组合在一起。