我想改变一些课程'来自代码的超类,最好的方法是什么?
我明白的一件事是
NewSuperclass subclass: #MyClass
"…with all the MyClass vars…"
但这看起来有点奇怪。
答案 0 :(得分:1)
这将有效,它将迁移类和实例......为什么你有疑虑?
答案 1 :(得分:1)
如上所述,最直接的方法是通过评估新的类定义来重新定义类。但是,以这种方式更改超类将导致编译器重新编译类及其子类中的所有代码。有时这可能是一个问题,例如重新定义系统在重新编译期间使用的核心类。
我有机会在昨天重新定义Float的超类,并且由于这个原因不得不避免重新编译。要完全更改超类,还需要从其旧父类的子类中删除该类,并将其添加到新父类的子类中,/和/必须使用类的元类执行该操作。所以这是一个完整的例子:
"Reparent the class and its metaclass"
Float superclass: LimitedPrecisionReal.
Float class superclass: LimitedPrecisionReal class.
"Remove the class and its metaclass from the subclasses of their old parent"
Number removeSubclass: Float.
Number class removeSubclass: Float class.
"Add the class and its metaclass to the subclasses of their new parent"
LimitedPrecisionReal addSubclass: Float.
LimitedPrecisionReal class addSubclass: Float class.
也许这应该是一个方法,比如changeSuperclassTo :?但是,记住,这很危险。 ClassBuilder在这个sutuation中重新编译代码是有充分理由的。更改超类而不重新编译仅在特殊情况下有效,不添加或删除inst vars,类的编译范围不变等。
答案 2 :(得分:0)
正如 Martin McClure 在邮件列表中所建议的那样:
看起来将#superclass:发送到班级就行了。