在这篇文章中,一位勇敢的人希望(在 C++ 中)将 Base
类型的对象向下转换为派生
类型。假设 Derived 类型的属性不超过 Base
,如果您嫉妒 Derived
类的额外方法,那么它可以有意义提供。
是否有编程语言允许这样的事情?
In this post, a brave wants (in C++) to downcast a object of type Base
to a Derived
type. Assuming that the Derived type has no more attributes than Base
, it can make sense if you're jealous of the extra methods that the Derived
class provides.
Are there programming languages that allow such a thing?
发布评论
评论(3)
实际上,这在 Common Lisp 以及移植了 CLOS(Common Lis 对象系统)的其他 Lisp 方言中是没有问题的。您使用
change-class
通用函数为此。CLOS 使用多个调度方法,因此方法不依赖于类或对象,它只是在一组与其参数的类型(或标识)类似的函数中选择的函数。使用
change-class
时,您可以像创建新实例一样给出参数,并且已存储在对象中的数据将保留。下面是一个小会话,展示了它是如何工作的:如果这个默认行为还不够,您可以在
更新不同类的实例
。是的,有一些编程语言允许这样的事情!
Actually, this is something that is done without problem in Common Lisp, and in other Lisp dialects where CLOS (Common Lis Object System) was ported. You use the
change-class
generic function for that.CLOS works with multiple dispatch methods, so a method is not tied to a class or object, it's just a function that is chosen in a group of similar functions WRT to the types (or identities) of its arguments. When using
change-class
, you can give arguments as if you were creating a new instance, and data already stored in the object will remain. Here is a little session that shows how it works:If this default behaviour is not enough, you may define a method on
update-instance-for-different-class
.So yeah, there are programming languages that allow such a thing!
不,但首先这样做就有很强烈的代码味道。
更好的替代方法是使用 装饰器模式,这正是它的用途。
No, but it's a strong code smell to do that in the first place.
A way better alternative is to use the decorator pattern, this is exactly what it was made to do.
如果
Derived
未添加任何属性,则它添加的方法必须在从Base
获取的状态上运行。在这种情况下,为什么不将这些方法移至它们所属的Base
位置呢?If
Derived
adds no attributes then the method it adds must operate on state that it gets fromBase
. In that case, why not just move those methods toBase
where they belong?