Objective-c 中继承实例变量
在 Objective-c 2.0 中,为什么子类需要使用 self 关键字引用父类中的实例变量?
考虑这个例子:
// a.h
@interface MyClass : NSObject
@property (nonatomic, retain) Object *myObject;
@end
// a.m
@implementation MyClass
@synthesize myObject;
@end
// b.h
@interface AnotherClass : MyClass
@end
// b.m
@implementation AnotherClass
- (void) someMethod {
// error
// Object *obj = myObject;
// works
// Object *obj = self.myObject;
}
@end
In Objective-c 2.0 why do subclasses need to reference instance variables in parent classes using the self
keyword?
Consider this example:
// a.h
@interface MyClass : NSObject
@property (nonatomic, retain) Object *myObject;
@end
// a.m
@implementation MyClass
@synthesize myObject;
@end
// b.h
@interface AnotherClass : MyClass
@end
// b.m
@implementation AnotherClass
- (void) someMethod {
// error
// Object *obj = myObject;
// works
// Object *obj = self.myObject;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上并没有定义变量,只是定义了一个属性(它隐式定义了一个私有变量)。由于属性只是方法,因此您需要点语法。请注意,
self.property
与[self property]
相同。要解决此问题,请指定一个变量。我将举一个例子,其中变量的名称与属性的名称不同。大多数人为两者选择相同的名字,但我喜欢让它们不同,这样我就能立即明白哪个是指哪个。
请注意分配时的差异:如果分配给变量,则对象不会自动保留。但如果您使用以下属性,它就会被保留:
编辑: 提到合成的实例变量默认情况下是私有的,正如 @Jason Coco 所指出的。 @NSGod 是对的,普通实例变量默认是受保护的,而不是公共的,修复了这个问题。
You haven't actually defined a variable, you only defined a property (which implicitly defines a variable that is private). And since property are just method, you need the dot syntax. Note that
self.property
is the same as[self property]
.To fix this, specify a variable. I'll give you an example where the variable has a different name than the property. Most people chose the same name for both but I like to have them differ so I immediately see which one is meant.
Note the difference when you assign: if you assign to your variable the object is not retained automatically. But it is retained if you use the property:
Edit: Mentioned that the synthesized instance variables are private by default, as noted by @Jason Coco. And @NSGod is right that normal instance variables are protected by default rather than public, fixed that.
如果您实际上在超类中声明了实例变量,而不是依赖新运行时合成实例变量的能力(除了合成访问器方法之外),则它们不会。请参阅 Objective-C 编程语言:运行时差异,了解有关实例变量综合的更多信息。
例如,为了能够直接引用实例变量,您需要将以下内容更改
为
:默认情况下,实例变量是
@protected
,这意味着该类和任何子类都可以访问直接实例变量。@protected
ivars 与@public
ivars 的不同之处在于您无法使用->
访问它们。@private
ivar 只能由声明它们的类访问。请参阅 Objective-C 编程语言:实例变量的范围了解更多信息。They don't, provided you actually declare an instance variable in the superclass, rather than rely on the new runtime's ability to synthesize the instance variable (in addition to synthesizing the accessor methods). See The Objective-C Programming Language: Runtime Difference for more info on instance variable synthesis.
For example, to be able to refer to the instance variable directly, you'd need to change the following:
to:
By default, instance variables are
@protected
, meaning the class and any subclasses can access the instance variables directly.@protected
ivars differ from@public
ivars in that you can't access them using->
.@private
ivars can only be accessed by the class that declares them. See The Objective-C Programming Language: The Scope of Instance Variables for more info.