Objective-C 中使用类别的私有方法:从子类调用 super
我正在阅读如何在 Objective-C 中实现私有方法(在 Objective-C 中为类定义私有方法的最佳方式),我的脑海中突然出现一个问题:
如何实现受保护的方法,即可见的私有方法假设
我有一个 MySuperClass,其类别包含其所有私有方法,并且我想实现一个 MySubclass 重写或调用 super 到 MySuperClass 私有方法之一。这可能吗(使用类别方法来实现私有方法)?
看一下这段代码,底部有重写方法。
// ===========================
// = File: MySuperClass.h
// = Interface for MySuperClass
// ===========================
@interface MySuperClass : Object
...
@end
// ===========================
// = File: MySuperClass.m
// ===========================
#import "MySuperClass.h"
// =================================
// = Interface for Private methods
// =================================
@interface MySuperClass (Private)
-(void) privateInstanceMethod;
@end
// =====================================
// = Implementation of Private methods
// =====================================
@implementation MySuperClass (Private)
-(void) privateInstanceMethod
{
//Do something
}
@end
// ================================
// = Implementation for MySuperClass
// ================================
@implementation MySuperClass
...
@end
// ===========================
// = File: MySubClass.h
// = Interface for MySubClass
// ===========================
@interface MySubClass : MySuperClass
...
@end
// ================================
// = Implementation for MySubClass
// ================================
#import MySubClass.h
@implementation MySubClass
//OVERRIDING a Super Private method.
-(void) privateInstanceMethod
{
[super privateInstanceMethod]; //Compiler error, privateInstanceMethod not visible!
//Do something else
}
@end
希望有人已经弄清楚了这一点。
干杯!
I was reading how to implement private methods in Objective-C (Best way to define private methods for a class in Objective-C) and a question popped up in my mind:
How do you manage to implement protected methods, i.e. private methods that are visible to subclasses?
Suppose I have a MySuperClass with a Category containing all its private methods, and I want to implement a MySubclass overriding or calling super to one of the MySuperClass private methods. Is that possible (using the Categories approach towards implementing private methods)?
Take a look at some of this code, at the bottom there is the overriden method.
// ===========================
// = File: MySuperClass.h
// = Interface for MySuperClass
// ===========================
@interface MySuperClass : Object
...
@end
// ===========================
// = File: MySuperClass.m
// ===========================
#import "MySuperClass.h"
// =================================
// = Interface for Private methods
// =================================
@interface MySuperClass (Private)
-(void) privateInstanceMethod;
@end
// =====================================
// = Implementation of Private methods
// =====================================
@implementation MySuperClass (Private)
-(void) privateInstanceMethod
{
//Do something
}
@end
// ================================
// = Implementation for MySuperClass
// ================================
@implementation MySuperClass
...
@end
// ===========================
// = File: MySubClass.h
// = Interface for MySubClass
// ===========================
@interface MySubClass : MySuperClass
...
@end
// ================================
// = Implementation for MySubClass
// ================================
#import MySubClass.h
@implementation MySubClass
//OVERRIDING a Super Private method.
-(void) privateInstanceMethod
{
[super privateInstanceMethod]; //Compiler error, privateInstanceMethod not visible!
//Do something else
}
@end
Hopefully somebody already figured this out.
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此 GNUStep 页面 描述了第 4.5 节中的一种方法:
This GNUStep page describes one approach Section 4.5:
在 Apple,当他们构建框架时,典型的模式是拥有一个公共标头 (MyClass.h) 和一个私有标头 (MyClass_private.h),并且仅将公共标头复制到构建产品中。当然,.m 文件将 #import 两者。
At Apple, when they build the frameworks the typical pattern is to have a public header (MyClass.h) and a private header (MyClass_private.h), and only copy the public headers into the build product. The .m file will #import both of them, of course.