获取小部件实例
如何从小部件实例获取类实例。在gwt中,我的小部件是pojo类的私有字段。我能够使用 instanceOfPojo.returnWidget() 获取小部件;方法。我可以知道如何获取小部件的实例类,以便我可以用于
if( widget instanceof CustomWidgetClass) ?
-- 让我重新表述一下
,AbcClass 扩展了 Composite,我们可以检查是否匹配
if(widget instanceof AbcClass) .
,但假设我们不知道 AbcClass 类存在,但我们可以获取 abcClass 的实例。有了abcClass的这个实例,如何使用java创建类似的类,我们将其称为DefClass,以便我们可以将abcClass转换为它?
how to get class intance from widget instance. in gwt, my widget is a private field of pojo class. i able to get the widget using instanceOfPojo.returnWidget(); method. May i know how to get instance class of the widget so that i can use for
if( widget instanceof CustomWidgetClass) ?
--
Let me rephrase
let say , AbcClass extends Composite, we can check whether match with
if(widget instanceof AbcClass) .
but let say we do not know the class AbcClass exist, but we can get instance of abcClass. with this instance of abcClass, how to use java to create similar class and we call it DefClass so that we can cast abcClass to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的小部件位于 POJO 内,那么我建议您重新审视您的设计。对于您想要做的事情来说,也许工厂会是更好的设计。
If you got your widget is inside your POJO then I would recommend you to revisit your design. Maybe a factory would be a better design for what you are trying to do.
我真的很难理解你的要求。您是否正在尝试获得与widget instanceof instanceOfPojo.returnWidget()
等效的工作,但它不起作用,因为instanceof
仅适用于类文字?查看如果
instanceOfPojo.returnWidget()
的类是widget
类的超类,则可以使用 isAssignableFrom:查看它们是否是完全相同的类:
如果这不是您的意思,请尝试澄清您的问题。编辑: OP 已经澄清了问题。
要获取类的实例,然后从中创建类似的类,需要在运行时动态创建一个类。在 Java 中动态创建一个类是可能的,但我非常怀疑你是否可以在 GWT 客户端代码中做到这一点。这是因为 GWT 仅支持 Java 的一个子集并被转换为 Javascript。
由于
abcClass
的类是(“未知”)类AbcClass
并且AbcClass
直接从 Composite 派生,因此您将无法进行强制转换abcClass
到此动态类DefClass
。DefClass
不是abcClass
继承层次结构的一部分(AbcClass
->Composite
->Widget< /code> ->
UIObject
->Object
),并且您无法在运行时更改继承层次结构以包含DefClass
。I'm really having a hard time understanding what you are asking for. Are you trying to get a working equivalent ofwidget instanceof instanceOfPojo.returnWidget()
, which doesn't work becauseinstanceof
only works with Class literals?To see if the class of
instanceOfPojo.returnWidget()
is a superclass ofwidget
's class, you could use isAssignableFrom:To see if they are the exact same class:
If that's not what you mean, please try to clarify your question.Edit: OP has since clarified the question.
To get an instance of a class and then create a similar class from it would require dynamically creating a class at run-time. Its possible in Java to dynamically create a class, but I very much doubt you could do it in GWT client code. This is because GWT supports only a subset of Java and gets translated to Javascript.
Since the class of
abcClass
is the ("unknown") classAbcClass
andAbcClass
derives directly from Composite, you won't be able to castabcClass
to this dynamic classDefClass
.DefClass
is not part ofabcClass
inheritance hierarchy (AbcClass
->Composite
->Widget
->UIObject
->Object
) and you can't change that inheritance hierarchy to includeDefClass
during runtime after the fact.