gpt4 book ai didi

Gwt - 如何将叠加层(JavaScriptObject) "cast"转换为真正的 Java 对象?

转载 作者:行者123 更新时间:2023-12-02 04:07:11 28 4
gpt4 key购买 nike

在我的 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();
}
}

关于Gwt - 如何将叠加层(JavaScriptObject) "cast"转换为真正的 Java 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7023295/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com