【发布时间】:2011-04-13 03:44:39
【问题描述】:
Objective-C 中的受保护方法等价于什么? 我想定义只有派生类可以调用/实现的方法。
【问题讨论】:
标签: objective-c
Objective-C 中的受保护方法等价于什么? 我想定义只有派生类可以调用/实现的方法。
【问题讨论】:
标签: objective-c
您既不能声明受保护的方法或私有。 Objective-C 的动态特性使得实现方法的访问控制成为不可能。 (你可以通过大量 修改编译器或运行时,以严重的速度损失,但显然没有这样做。)
取自Source。
【讨论】:
您可以通过执行以下操作模拟对方法的受保护和私有访问:
正如 Sachin 所指出的,这些保护措施不会在运行时强制执行(例如在 Java 中)。
【讨论】:
UIGestureRecognizerSubclass.h 中一样实现东西
一种选择是使用类扩展来隐藏方法。
在.h:
@interface SomeAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
在.m:
@interface SomeAppDelegate()
- (void)localMethod;
@end
@implementation SomeAppDelegate
- (void)localMethod
{
}
@end
【讨论】:
@interface 声明。您可以只声明一个函数并使用它,它将被视为私有。
这是我所做的,以使受保护的方法对我的子类可见,而无需它们自己实现方法。这意味着我没有在我的子类中收到关于实现不完整的编译器警告。
SuperClassProtectedMethods.h(协议文件):
@protocol SuperClassProtectedMethods <NSObject>
- (void) protectMethod:(NSObject *)foo;
@end
@interface SuperClass (ProtectedMethods) < SuperClassProtectedMethods >
@end
SuperClass.m:(编译器现在会强制你添加受保护的方法)
#import "SuperClassProtectedMethods.h"
@implementation SuperClass
- (void) protectedMethod:(NSObject *)foo {}
@end
子类.m:
#import "SuperClassProtectedMethods.h"
// Subclass can now call the protected methods, but no external classes importing .h files will be able to see the protected methods.
【讨论】:
performSelector 就可以了。
[(id)obj hiddenMethod]。准确地说,Objective-C 不支持受保护的方法。
可以将方法定义为父类的私有方法,在子类中可以使用[super performSelector:@selector(privateMethod)];。
【讨论】:
您可以在某种程度上对类别进行此操作。
@interface SomeClass (Protected)
-(void)doMadProtectedThings;
@end
@implementation SomeClass (Protected)
- (void)doMadProtectedThings{
NSLog(@"As long as the .h isn't imported into a class of completely different family, these methods will never be seen. You have to import this header into the subclasses of the super instance though.");
}
@end
如果您在另一个类中导入类别,则不会隐藏这些方法,但您只是没有。由于 Objective-C 的动态特性,实际上不可能完全隐藏方法而不管调用实例类型如何。
最好的方法可能是@Brian Westphal 回答的类延续类别,但您必须为每个子类实例重新定义该类别中的方法。
【讨论】:
我刚刚发现了这一点,它对我有用。为了改进亚当的回答,在你的超类中实现 .m 文件中的受保护方法,但不要在 .h 文件中声明它。在您的子类中,在您的 .m 文件中创建一个新类别,并声明超类的受保护方法,您可以在子类中使用超类的受保护方法。如果在运行时强制,这最终不会阻止所谓受保护方法的调用者。
/////// SuperClass.h
@interface SuperClass
@end
/////// SuperClass.m
@implementation SuperClass
- (void) protectedMethod
{}
@end
/////// SubClass.h
@interface SubClass : SuperClass
@end
/////// SubClass.m
@interface SubClass (Protected)
- (void) protectedMethod ;
@end
@implementation SubClass
- (void) callerOfProtectedMethod
{
[self protectedMethod] ; // this will not generate warning
}
@end
【讨论】:
protectedMethod的警告
使用@protected 变量的另一种方式。
@interface SuperClass:NSObject{
@protected
SEL protectedMehodSelector;
}
- (void) hackIt;
@end
@implementation SuperClass
-(id)init{
self = [super init];
if(self) {
protectedMethodSelector = @selector(baseHandling);
}
return self;
}
- (void) baseHandling {
// execute your code here
}
-(void) hackIt {
[self performSelector: protectedMethodSelector];
}
@end
@interface SubClass:SuperClass
@end
@implementation SubClass
-(id)init{
self = [super init];
if(self) {
protectedMethodSelector = @selector(customHandling);
}
return self;
}
- (void) customHandling {
// execute your custom code here
}
@end
【讨论】:
我通常用内部前缀命名受保护的方法:
-(void) internalMethod;
【讨论】: