且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JAVA中实例化接口?

更新时间:2023-02-19 22:49:58

GWT.create

在编译时由GWT Java编译器专门处理。 GWT 类是Google将低级魔术加入到GWT中的地方。

在这个问题下的更多细节。


I'm reading the docs on the UIBinder of GWT and the first code snippet made me confused:

public class HelloWorld extends UIObject { // Could extend Widget instead
  interface MyUiBinder extends UiBinder<DivElement, HelloWorld> {}
  private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

  @UiField SpanElement nameSpan;

  public HelloWorld() {
    // createAndBindUi initializes this.nameSpan
    setElement(uiBinder.createAndBindUi(this));
  }
}

On the second line an interface is created locally which extends the UiBinder interface. However, on the third line an instance of this interface is created using GWT.create().

How is this possible? There's nowhere a class that implements MyUiBinder, so it can't be instantiated, right?

GWT.create is treated specially by the GWT Java compiler at compile time. The GWT class is the place where Google puts the low-level "magic" that makes GWT work.

More details under this question.