简体   繁体   English

gwt-如何在gwt中继承?

[英]gwt - How does the inheritance work in gwt?

Forgive me if my question is too naive as I just started working with gwt. 如果我刚开始使用gwt时我的问题太幼稚,请原谅我。

I create a gwt module (let's say Editor_A ) which has a number of DataGrid, buttons, etc. It works fine in the web page. 我创建了一个gwt module (比如说Editor_A ),该gwt module具有许多DataGrid,按钮等。它在网页中运行良好。

Then I am about to create the 2nd gwt module Editor_B . 然后,我将创建第二个gwt模块Editor_B

They will not be on the same page ever, however, Editor_B is very similar with Editor_A as it just has an extra table and a button. 它们永远不会在同一页面上,但是, Editor_BEditor_A非常相似,因为它只有一个额外的表和一个按钮。

Now of course, I don't want to copy/paste code from Editor_A to Editor_B . 当然,现在,我不想将代码从Editor_A 复制/粘贴Editor_B Instead, I wish to reuse the code from Editor_A . 相反, 我希望重用Editor_A中的代码

How can I do that? 我怎样才能做到这一点?

How can I inherit from Editor_A and reuse most of its code with some modification to produce Editor_B? 如何从Editor_A继承并重用其大部分代码,并进行一些修改以产生Editor_B?

This can solve your problem: You can create an additional Core module, which is responsible for your base custom widgets, Client-Side Utils etc. Then you should inherit Core module in your both modules. 这可以解决您的问题:您可以创建一个附加的Core模块,该模块负责您的基本自定义小部件,客户端Utils等。然后您应该在这两个模块中继承Core模块。 So in your case you can create Composite widget which is used by Editor_A , then in your latter module you can create your Editor_B by inheriting from Editor_A . 因此,在您的情况下,您可以创建由Editor_A使用的Composite小部件,然后在后面的模块中,您可以通过继承Editor_A来创建Editor_B And of course you can add some modifications to that from Editor_B class. 当然,您可以在Editor_B类中对其进行一些修改。

If you are using they in the same module, you can create one BaseEditor which is Editor_A in your case. 如果在同一模块中使用它们,则可以创建一个BaseEditor,在您的情况下为Editor_A。 And then: 接着:

public class Editor_B extends Editor_A{
    public Editor_B(){
      draw();
    }

    private void draw(){
   // here, add your extra table and button, to panel which're protected(public)
    }
}

Apart from Java Inheritance be sure to configure GWT modules in the appropriate way, else the GWT Java to Javascript compiler will not find the relevant classes. 除了Java继承,请确保以适当的方式配置GWT模块,否则GWT Java到Javascript编译器将找不到相关的类。 Do this by modifying the module XML configuration to be something like this: 通过将模块XML配置修改为以下方式来执行此操作:

<module>
   <inherits name='com.google.gwt.user.User'/>
   <source path="widgets"/>
</module>

The best approach is to create custom widgets that you can reuse easily in the same module or in different modules. 最好的方法是创建可在同一模块或不同模块中轻松重用的自定义窗口小部件。 Then, you can either: 然后,您可以:

(a) extend a custom widget by adding more child widgets and/or new functionality (MyBigPanel extends MySmallPanel), or (a)通过添加更多子控件和/或新功能来扩展自定义控件(MyBigPanel扩展了MySmallPanel),或者

(b) simply add more children to the same composite widget (provided it can take them, ie you used a LayoutPanel, FlowPanel, HtmlPanel or the like as a container for your custom widget). (b)只需将更多子代添加到同一个复合窗口小部件(前提是可以容纳它们,即您将LayoutPanel,FlowPanel,HtmlPanel等用作自定义窗口小部件的容器)。 Then you simply call mySmallPanel.add(anotherButton). 然后,您只需调用mySmallPanel.add(anotherButton)。

A side note: You mentioned that these widgets are not going to be used on the same page. 附带说明:您提到这些小部件将不在同一页面上使用。 It does not mean they have to be in different modules. 这并不意味着它们必须位于不同的模块中。 They can be used in different views in the same module. 它们可以在同一模块的不同视图中使用。 Think of modules as of separate apps that have different users, for example. 例如,将模块视为具有不同用户的独立应用程序。 In this case you can abstract all your widgets into a different module (and inherit it in other modules) or you can put them in a separate jar. 在这种情况下,您可以将所有小部件抽象到另一个模块中(并在其他模块中继承),也可以将它们放在单独的jar中。

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

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