简体   繁体   English

在GWT中,为什么约定使用asWidget()而不是强制转换?

[英]In GWT, why is the convention to use asWidget() instead of casting?

I'm new to GWT. 我是GWT的新手。 In the code examples I've seen, the presenter defines an interface Display that includes the asWidget method. 在我看过的代码示例中,演示者定义了一个包含asWidget方法的接口Display。 For example, 例如,

public interface Display {
   HasClickHandlers getSaveButton();
   HasValue<String> getName();
   ...
   Widget asWidget();
}

Then the presenter implements the go() method: 然后,演示者实现go()方法:

public void go(final HasWidgets container){
   ...
   container.add(display.asWidget());
}

What's wrong with just casting the display to a Widget, eg 将显示器转换为Widget有什么问题,例如

container.add((Widget) display);

Thank you! 谢谢!

asWidget() is in GWT in order to facilitate testing. asWidget()在GWT中以便于测试。

With it you can use stub implementations of displays and other view components in your tests without having to extends the abstract class Widget. 有了它,您可以在测试中使用显示和其他视图组件的存根实现,而无需扩展抽象类Widget。

So in your case, your display can be any class, not necessarily a Widget, the only condition is that it returns a Widget. 所以在你的情况下,你的显示器可以是任何类,不一定是Widget,唯一的条件是它返回一个Widget。

Here you can read a more complete explanation. 在这里,您可以阅读更完整的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM