当前位置: 动力学知识库 > 问答 > 编程问答 >

gwt - Wrapping JavaScriptObjects in case of multiple subclasses?

问题描述:

I have a bunch of data entities that all implement Entity. Now I want to expose some of these entities to JavaScript code, but I can't just make a bunch of JavaScriptObject subclasses because of the one-implementation rule.

So, I'm using this kind of thing:

public class JsStandardScale3 implements StandardScale3 {

private JavaScriptObject wrapped;

public JsStandardScale3(JavaScriptObject wrapped) {

this.wrapped = wrapped;

}

@Override

public native Long getLicenseId() /*-{

[email protected]::wrapped.getLicenseId();

}-*/;

This works, it's just a lot of work. The overlay type structure is so much nicer. Any suggestions?

网友答案:

It turns out that you CAN extend JavaScriptObject with multiple subclasses of an interface as long as all of your extensions are from a single "root" extension of JSO.

For example, I have the structure Standard extends Entity and Course extends Entity. I could NOT do:

JsStandard extends JavaScriptObject...
JsCourse extends JavaScriptObject...

but I could do:

JsEntity extends JavaScriptObject...
JsStandard extends JsEntity...
JsCourse extends JsEntity...

fantastic!

The only limitation is that every method must be marked final, which works fine for a simple overlay scenario.

分享给朋友:
您可能感兴趣的文章:
随机阅读: