获取小部件实例

发布于 2024-10-14 20:18:45 字数 428 浏览 2 评论 0原文

如何从小部件实例获取类实例。在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日记撕了你也走了 2024-10-21 20:18:45

如果您的小部件位于 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.

深居我梦 2024-10-21 20:18:45

...那么与 if( widget instanceof instanceOfPojo.returnWidget()) 相比,我如何使用它?

我真的很难理解你的要求。您是否正在尝试获得与 widget instanceof instanceOfPojo.returnWidget() 等效的工作,但它不起作用,因为 instanceof 仅适用于类文字?

查看如果 instanceOfPojo.returnWidget() 的类是 widget 类的超类,则可以使用 isAssignableFrom

instanceOfPojo.returnWidget().getClass().isAssignableFrom(widget.getClass());

查看它们是否是完全相同的类:

instanceOfPojo.returnWidget().getClass() == widget.getClass()

如果这不是您的意思,请尝试澄清您的问题。

编辑: OP 已经澄清了问题。

...我们可以获得abcClass的实例。有了abcClass这个实例,如何使用java创建类似的类

要获取类的实例,然后从中创建类似的类,需要在运行时动态创建一个类。在 Java 中动态创建一个类是可能的,但我非常怀疑你是否可以在 GWT 客户端代码中做到这一点。这是因为 GWT 仅支持 Java 的一个子集并被转换为 Javascript。

...我们将其称为 DefClass,以便我们可以将 abcClass 转换为它?

由于 abcClass 的类是(“未知”)类 AbcClass 并且 AbcClass 直接从 Composite 派生,因此您将无法进行强制转换abcClass 到此动态类 DefClassDefClass 不是 abcClass 继承层次结构的一部分(AbcClass -> Composite -> Widget< /code> -> UIObject -> Object),并且您无法在运行时更改继承层次结构以包含 DefClass

... so how to i use it compared with if( widget instanceof instanceOfPojo.returnWidget()) ?

I'm really having a hard time understanding what you are asking for. Are you trying to get a working equivalent of widget instanceof instanceOfPojo.returnWidget(), which doesn't work because instanceof only works with Class literals?

To see if the class of instanceOfPojo.returnWidget() is a superclass of widget's class, you could use isAssignableFrom:

instanceOfPojo.returnWidget().getClass().isAssignableFrom(widget.getClass());

To see if they are the exact same class:

instanceOfPojo.returnWidget().getClass() == widget.getClass()

If that's not what you mean, please try to clarify your question.

Edit: OP has since clarified the question.

... we can get instance of abcClass. with this instance of abcClass, how to use java to create similar class

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.

... we call it DefClass so that we can cast abcClass to it?

Since the class of abcClass is the ("unknown") class AbcClass and AbcClass derives directly from Composite, you won't be able to cast abcClass to this dynamic class DefClass. DefClass is not part of abcClass inheritance hierarchy (AbcClass -> Composite -> Widget -> UIObject -> Object) and you can't change that inheritance hierarchy to include DefClass during runtime after the fact.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文