在GWT中,为什么不能返回一个接口? [英] In GWT, why shouldn't a method return an interface?

查看:102
本文介绍了在GWT中,为什么不能返回一个接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google IO 2009中的这段视频中,演示者很快就表示方法的签名应该返回具体类型而不是接口。



从我在视频中听到的内容来看,这与GWT Java-to- Javascript编译器。




  • 这种选择背后的原因是什么?


  • 什么方法可以返回接口而不是具体类型,哪些返回具体实例会更好?



解决方案

正如你所说的,这与gwt编译器有关。 编辑:但是,正如Daniel在下面的评论中指出的那样,这通常不适用于gwt编译器,而只适用于使用GWT-RPC。



如果您声明List而不是ArrayList作为返回类型,那么gwt-compiler将在您的编译代码中包含完整的List层次结构(即实现List的所有类型)。如果使用ArrayList,编译器只需要包含ArrayList层次结构(即实现ArrayList的所有类型 - 通常只是ArrayList本身)。使用一个接口而不是一个具体的类,你将支付在编译时间和生成的代码的大小(并且因此每个用户在运行你的应用程序时必须下载的代码量)的惩罚。



你也在问这个原因:如果你使用接口(而不是具体类),编译器在编译时不知道这些接口的哪些实现将被使用。因此,它包含了所有可能的实现。



关于你最后的问题:所有方法都可以被声明为返回接口(这就是你想要的,对吗?)。然而,上述惩罚适用。

顺便说一句:据我了解,这个问题不仅限于方法。它适用于所有类型声明:变量,参数。无论何时使用接口声明某些东西,编译器都会包含完整的子接口层次结构和实现类。 (很明显,如果你只用一个或两个实现类来声明自己的接口,那么你不会承担很大的处罚,这就是我在GWT中使用接口的方式。)



简而言之:尽可能使用具体的类。



(小建议:如果您在引用视频时给出了时间戳,这将有所帮助。)

b $ b

In this video from Google IO 2009, the presenter very quickly says that signatures of methods should return concrete types instead of interfaces.

From what I heard in the video, this has something to do with the GWT Java-to-Javascript compiler.

  • What's the reason behind this choice ?
  • What does the interface in the method signature do to the compiler ?
  • What methods can return interfaces instead of concrete types, and which are better off returning concrete instances ?

解决方案

This has to do with the gwt-compiler, as you say correctly. EDIT: However, as Daniel noted in a comment below, this does not apply to the gwt-compiler in general but only when using GWT-RPC.

If you declare List instead of ArrayList as the return type, the gwt-compiler will include the complete List-hierarchy (i.e. all types implementing List) in your compiled code. If you use ArrayList, the compiler will only need to include the ArrayList hierarchy (i.e. all types implementing ArrayList -- which usually is just ArrayList itself). Using an interface instead of a concrete class you will pay a penalty in terms of compile time and in the size of your generated code (and thus the amount of code each user has to download when running your app).

You were also asking for the reason: If you use the interface (instead of a concrete class) the compiler does not know at compile time which implementations of these interfaces are going to be used. Thus, it includes all possible implementations.

Regarding your last question: all methods CAN be declared to return interface (that is what you ment, right?). However, the above penalty applies.

And by the way: As I understand it, this problem is not restricted to methods. It applies to all type declarations: variables, parameters. Whenever you use an interface to declare something, the compiler will include the complete hierarchy of sub-interfaces and implementing classes. (So obviously if you declare your own interface with only one or two implementing classes then you are not incurring a big penalty. That is how I use interfaces in GWT.)

In short: use concrete classes whenever possible.

(Small suggestion: it would help if you gave the time stamp when you refer to a video.)

这篇关于在GWT中,为什么不能返回一个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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