在JAVA中实例化接口? [英] Instantiate interface in JAVA?

查看:158
本文介绍了在JAVA中实例化接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于GWT的 UIBinder 的文档,第一个代码片段让我感到困惑:

  public class HelloWorld extends UIObject {//可以扩展Widget而不是
接口MyUiBinder扩展了UiBinder< DivElement,HelloWorld> {}
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

@UiField SpanElement nameSpan;

public HelloWorld(){
// createAndBindUi初始化this.nameSpan
setElement(uiBinder.createAndBindUi(this));






$ b

在第二行上创建一个本地接口, UiBinder 界面。但是,在第三行中,使用 GWT.create()创建此接口的实例。

这怎么可能?没有一个类实现 MyUiBinder ,所以它不能被实例化,对吗?

解决方案

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

< a href =https://stackoverflow.com/questions/451658/gwt-dynamic-loading-using-gwt-create-with-string-literals-instead-of-class-lite>在这个问题下的更多细节。


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.

这篇关于在JAVA中实例化接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆