我正在学习客观的C语言,我问一个简单的问题, 当我这样做时:
// ParentClass.h
@interface ParentClass : NSObject
@property (read, strong) NSString *parentPublicStr;
@end
// ParentClass.m
@interface ParentClass ()
@property (readwrite, strong) NSString *parentPrivateStr;
@end
@implementation ParentClass
@synthesize parentPublicStr;
@synthesize parentPrivateStr;
@end
// Subclass SubClass.h
@interface SubClass : ParentClass
- (void) test;
@end
@implementation SubClass
- (void) test
{
// Its not possible to do that : [self setParentPrivateStr:@"myStrin"]
// And for parentPublicStr, it is public property so not protected, because i can change the value
// in main.c, and it's so bad..
}
@end
我想创建一个受保护的属性:x
谢谢你。 (对不起我的英文)
答案 0 :(得分:2)
答案 1 :(得分:0)
只要您使用带有下划线的相同名称,您就可以手动为该属性创建一个ivar:
@interface ParentClass : NSObject
{
@protected
NSString* _parentPublicStr;
}
@property (read, strong) NSString *parentPublicStr;
@end
这使得@protected属性的合成ivar(默认为@private),然后子类可以直接使用超类'ivar。