繁体   English   中英

有没有一种方法可以使用GWT.create(…)实例化泛型类型的类?

[英]Is there a way to use GWT.create(…) to instantiate a class of a generic type?

有没有办法我可以做这样的事情:

public class Foo<Bar> {
    public Bar newBar() {
        return GWT.create( /* something useful here */ );
    }
}

这项工作是否可行,例如:

public class Foo<Bar> {
    public Bar newBar(Class<Bar> prototype) {
        return GWT.create(prototype);
    }
}

例如我在哪里做new Foo<MyBar>().newBar(MyBar.class)

但是,理想情况下,我什至不需要传递原型对象。

在这种情况下,不会。 必须始终使用实际的类文字来调用GWT.create方法,而永远不要使用变量/字段/参数。 这是因为GWT.create实际上是一种new的特殊形式,没有参数,并且也不适用于变量(无反射):

Class<? extends Bar> clazz = ...;
Bar instance = new clazz;//nope

GWT.create方法看起来像一个方法调用,在Java中确实如此,但是当您编译为JS时,必须遵循给定排列的特定延迟绑定规则,将其转换为构造函数调用。

暂无
暂无

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

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