哪里允许在运行时更改对象的类

发布于 2024-10-31 21:16:39 字数 292 浏览 2 评论 0原文

您是否知道允许(支持)在运行时更改对象类的编程语言?

请给出有关语法的简短示例。如果您知道的话,请提供一个用例。涉及鸭子类型的例子也很受欢迎,所以不要回避提及这些语言。

更新:我发现Smalltalk有changeClassTobecome。 CLOS 可以进行change-class。我发现一篇论文建议使用这些机制来实现在运行时引用的“外壳对象”,但仅在实际访问时从某些持久性构建,从而提供一些相关对象的漂亮延迟加载。

Do you know programming languages where changing the class of an object at runtime is allowed (supported)?

Please give a short example regarding the syntax. Give a use case, if you know any. Examples involving duck typing are welcome as well, so do not shy away from mentioning these languages.

Update: I figured out that Smalltalk has changeClassTo and become. CLOS can do change-class. I found a paper suggesting to use these mechanisms to implement 'husk objects' that are referenced at runtime, but only constructed from some persistence when actually accessed, providing some nifty lazy loading of related objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

祁梦 2024-11-07 21:16:39

我假设,您的意思如下:

您有一个 A 类的对象。但是您想将其视为 B 类的对象。

有一些可能的构造:

  1. 如果 B 是 A 的子类,您可以将对象到B(但它应该创建为B,否则你会得到意想不到的(并且希望不想要的)结果)。

  2. 在某些语言中,您可以将任何内容转换为任何内容。如果您知道自己在做什么,那就太好了,否则请做好脚上的几个洞的准备。

你提到了鸭式打字。我没有这方面的实际经验。但据我所知,鸭子类型是这样的:“我需要一个支持方法 X、Y 和 Z 的对象。”在这种情况下,你不关心班级。你只是想让它按照你的命令嘎嘎叫、游泳和行走。

I assume, you mean the following:

You have an object of class A. But you would like to treat it as an object of class B.

There are some constructions possible:

  1. If B is a subclass of A you can cast the object to B (but it should be created as B else you have unexpected (and hopefully unwanted) results).

  2. In some languages you can cast anything to anything. If you know what you are doing, this is great, else prepare for several holes in your foot.

You mention ducktyping. I have no practical experience with it. But As far as I know, duck typing is something like this: "I need an object that support methods X, Y and Z." In that case you don't care about the class. You just want it to quack, swim and walk at your command.

梦回梦里 2024-11-07 21:16:39
Give a usecase

???我希望您针对特定用例寻求解决方案。
改变对象的类型?我认为“不”。

但是,如果您想更改对象的部分功能或行为,请查看松散耦合!
例如,您的类拥有 File_Saver 类型的成员。有一个公共 setter 接受 File_Saver 的任何实例,并且您可以注入 File_Saver_XML、File_Saver_PDF...

这不是常见的方法,但是类内部的任何处理都可以由 1-n 个松散耦合的处理程序完成,您可以从外部交换这些处理程序。

认真回答你的问题:你需要一个包装器+一个设置器。 :-)

Give a usecase

??? I'd expect you to ask for a solution on a specific use case.
Changing type of an object? I think "No."

But if you like to change part of an objects capabilities or behaviours have a look at loosely coupling!
For example your class holds a member of type File_Saver. There's a public setter accepting any instance of File_Saver and you can inject File_Saver_XML, File_Saver_PDF, ...

It's no common way, but any processing inside a class can be done by 1-n loosely coupled handlers, which you can exchange from outside.

Melt down to your question: You need a wrapper + a setter. :-)

许你一世情深 2024-11-07 21:16:39

一段时间后回到这个案例,我得出的结论是,如果您觉得需要更改对象类,则需要鸭子类型。

Coming back to the case after some time, I've come to the conclusion that you want duck typing if you feel the need of changing an objects class.

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