【问题标题】:Instantiate interface in JAVA?在JAVA中实例化接口?
【发布时间】:2010-09-30 10:19:26
【问题描述】:

我正在阅读 GWT 的 UIBinder 上的文档,第一个代码 sn-p 让我感到困惑:

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));
  }
}

在第二行中,在本地创建了一个扩展UiBinder 接口的接口。然而,在第三行,这个接口的一个实例是使用GWT.create() 创建的。

这怎么可能?没有实现MyUiBinder的类,所以不能实例化吧?

【问题讨论】:

    标签: java gwt interface instantiation


    【解决方案1】:

    GWT.create 在编译时被 GWT Java 编译器特殊处理。 GWT 类是 Google 放置使 GWT 工作的低级“魔法”的地方。

    More details under this question.

    【讨论】:

      【解决方案2】:

      这是一个相当 old answer(在 SO 术语中是旧的;))涵盖了相同的主题。

      据我了解:它不能神奇地工作,但在生成器的帮助下,该生成器知道如何为给定接口创建实例(在您的示例中是一些 MyUIBinderGenerator)。并且该生成器必须在一些 gwt.xml 文件中实现和发布。

      【讨论】:

        猜你喜欢
        • 2013-05-20
        • 1970-01-01
        • 1970-01-01
        • 2022-01-06
        • 1970-01-01
        • 1970-01-01
        • 2019-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多