在我的 GWT 项目中,我确实需要在多个叠加层(JavaScriptObject)之间实现相同的接口(interface)。
根据文档:
Starting with GWT 2.0, it is permissible for JavaScriptObject subtypes to implement interfaces. Every method defined in an interface may map to at most one method declared in a JavaScriptObject subtype. Practically speaking, this means that only one JavaScriptObject type may implement any given interface, but any number of non-JavaScriptObject types may also implement that interface.
所以,我想将我的 JavaScriptObject“转换”为一个真正的 Java 对象。它使我可以大大减少大量重复代码。
是否可以 ?
谢谢。
请您参考如下方法:
如何创建包装 JSO 而不是继承它的类?
public class PersonJso extends JavaScriptObject{
protected PersonJso() {}
public static native PersonJso create(String name) /*-{
return {name : name};
}-*/;
public final native String getName() /*-{
return this.name;
}-*/;
}
public class AnimalJso extends JavaScriptObject{
protected AnimalJso() {}
public static native PersonJso create(String name) /*-{
return {name : name};
}-*/;
public final native String getName() /*-{
return this.name;
}-*/;
}
public class AnimalWrapper implements hasName{
AnimalJso jso;
public AnimalWrapper(){}
public AnimalWrapper(AnimalJso jso){
this.jso = jso;
}
@Override
public String getName() {
return jso.getName();
}
}
public class PersonWrapper implements hasName{
PersonJso jso;
public PersonWrapper(){}
public PersonWrapper(PersonJso jso){
this.jso = jso;
}
@Override
public String getName() {
return jso.getName();
}
}