且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以使用类别覆盖超类中本身属于类别的方法吗?

更新时间:2022-12-04 23:11:50

虽然找不到参考文献,但我认为很明显, MySubclass(MyOtherCategory)中的实现优先。类别的方法已添加到该特定类中,因此 MyOtherCategory实现将属于您的子类,并且在消息分发期间继续进行父类查找之前将对其进行查找。

Although I can't find a reference, I think it's clear that the implementation in MySubclass (MyOtherCategory) takes precedence. The category's methods are added to that particular class, so the "MyOtherCategory" implementation will belong to your subclass, and it will be looked up before going on to the superclass during message dispatch.

正如Objective-C手册中指出的那样,可可中的许多方法都归类。如果查找顺序定义不正确,则您将无法覆盖子类类别中的所有这些方法,这将使类别几乎无用。

As pointed out in the Objective-C manual, a lot of methods in Cocoa are placed in categories. If the order of look-up wasn't well defined, you couldn't override any of those methods in categories on subclasses, which would make categories almost useless.