2

我有两个 Objective-C 类,一个是从另一个派生的

@interface DerivedClass : BaseClass
{
}

下面的代码部分属于BaseClass:

- (id)init {
    if (self = [super init]) {
       [self configure]; 
    }   
    return self;
}

- (void) configure{} //this is an empty method

并且代码部分属于DerivedClass:

-(void) configure{
    NSLog(@"derived configure called");
}

现在,当我说derivedInstance = [DerivedClass new];并观察调用堆栈时,我看到configure派生类[self configure]的方法在基类方法的行被调用init

我是一个 Objective-C 菜鸟,我对如何从基类的方法调用派生类的方法感到困惑。“ self”关键字被解释为与this某些语言的“ ”关键字相同,但我认为这种解释并不完全正确,对吧?

4

1 回答 1

6

[self someMessage]会将消息“someMessage”发送到当前对象,该对象是DerivedClass.

Message dispatch is done dynamically at run-time, so it will behave as whatever the object is at that time.

于 2011-03-30T11:29:49.063 回答