我不知道如果是在OBJ-C可以从一个属性存取权限父实例,如:在obj-c中可以获取属性的父实例?
@interface AObject : NSObject {
}
-(void) sample{
NSLog("%@", [[self parentInstance] Id]);
}
@interface DbObject : NSObject {
NSInteger Id;
AObject* ob;
}
我不知道如果是在OBJ-C可以从一个属性存取权限父实例,如:在obj-c中可以获取属性的父实例?
@interface AObject : NSObject {
}
-(void) sample{
NSLog("%@", [[self parentInstance] Id]);
}
@interface DbObject : NSObject {
NSInteger Id;
AObject* ob;
}
你必须让AObject
意识到DbObject
,但你也应该使用选择器访问DbObject
的属性。
@interface AObject : NSObject
{
id father;
}
- (id) initWithFather:(id) theFather;
- (void) sample;
@end
@implementation AObject
- (id) initWithFather:(id) theFather
{
father = theFather;
return self;
}
- (void) sample
{
NSLog (@"%d", [father Id]);
}
@end
@interface DbObject : NSObject
{
NSInteger Id;
}
- (id) initWithId:(NSInteger) anId;
- (NSInteger) Id;
@end
@implementation DbObject
- (id) initWithId:(NSInteger) anId;
{
Id = anId;
return self;
}
- (NSInteger) Id
{
return Id;
}
@end
int main (int argc, char *argv[])
{
DbObject *myDbObject = [[DbObject alloc] initWithId:5];
AObject *myAObject = [[AObject alloc] initWithFather:myDbObject];
[AObject sample];
return 0;
}
我不完全知道你是问什么。您是否在寻找关键字super
?
不是我所知道的。你必须让AObject知道它的父项。如果你操纵运行时,可能还有其他方法可以做到这一点,但是让AObject需要一个父代更容易。
不,超级是为了上课。
我问的是如何获取父对象(如在树中)。
但我认为这是不可能的...并且需要将孩子与父母联系起来。
如果你真的想这样做,你可以重写AObject中的“retain”方法来查看当前堆栈并查找调用类的ID。对不起,你必须研究如何做到这一点,我只是知道你可以从其他研究。
如果你真的想要做的是有某种调试语句,告诉你谁拥有一个特定的对象 - 你可以覆盖保留和设置断点或把有用的数据转储在那里。