2009-01-12 87 views
0

我不知道如果是在OBJ-C可以從一個屬性存取權限父實例,如:在obj-c中可以獲取屬性的父實例?

@interface AObject : NSObject { 
} 

-(void) sample{ 
NSLog("%@", [[self parentInstance] Id]); 
} 

@interface DbObject : NSObject { 
    NSInteger Id; 
    AObject* ob; 
} 

回答

0

你必須讓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; 
} 
0

我不完全知道你是問什麼。您是否在尋找關鍵字super

1

不是我所知道的。你必須讓AObject知道它的父項。如果你操縱運行時,可能還有其他方法可以做到這一點,但是讓AObject需要一個父代更容易。

0

不,超級是爲了上課。

我問的是如何獲取父對象(如在樹中)。

但我認爲這是不可能的...並且需要將孩子與父母聯繫起來。

0

如果你真的想這樣做,你可以重寫AObject中的「retain」方法來查看當前堆棧並查找調用類的ID。對不起,你必須研究如何做到這一點,我只是知道你可以從其他研究。

如果你真的想要做的是有某種調試語句,告訴你誰擁有一個特定的對象 - 你可以覆蓋保留和設置斷點或把有用的數據轉儲在那裏。

相關問題