下面是来自 Google's UIBinder
tutorial 的一些 Java 代码。此代码与单独的 HTML 页面一起显示文本“Hello, World”。
public class HelloWorld {
interface MyUiBinder extends UiBinder<DivElement, HelloWorld> {}
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
@UiField SpanElement nameSpan;
public HelloWorld() {
setElement(uiBinder.createAndBindUi(this));
}
public void setName(String name) {
nameSpan.setInnerText(name);
}
/**
* Method in question
*/
public void Element getElement() {
return nameSpan;
}
}
getElement() 方法具有 void 返回类型,但返回名为 nameSpan
的 Element
。鉴于它具有 void
返回类型,这怎么可能?
最佳答案
解释很简单,文档中的示例“有点”损坏。
如果示例只是扩展了 UIObject,那么 setElement()
和 getElement()
的实现就没有必要了
public class HelloWorld extends UIObject {
private static HelloWorldUiBinder uiBinder =
GWT.create(HelloWorldUiBinder.class);
interface HelloWorldUiBinder extends UiBinder<Element, HelloWorld> {
}
@UiField
SpanElement nameSpan;
public HelloWorld() {
setElement(uiBinder.createAndBindUi(this));
}
public void setName(String name) {
nameSpan.setInnerText(name);
}
}
<小时/>
顺便说一句,这是 UiBinder“hello world”示例的独立变体(作为第一个 UiBinder 示例可能更容易理解):
public class HelloWorld implements EntryPoint {
interface HelloWorldUiBinder extends UiBinder<Element, HelloWorld> {
}
@UiField SpanElement nameSpan;
public void onModuleLoad() {
final HelloWorldUiBinder uiBinder = GWT.create(HelloWorldUiBinder.class);
final Element element = uiBinder.createAndBindUi(this);
nameSpan.setInnerText("world");
Document.get().getBody().appendChild(element);
}
}
关于java - GWT UIBinder 方法返回类型 void?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12122684/