问题描述:

I have a method call in a base class that looks like this:

#import <Foundation/Foundation.h>

@interface BaseController : NSObject

-(void)login;

@end


#import "BaseController.h"

@implementation BaseController

-(void)performTask

{

return @"Base method loaded";

}

-(void)login

{

[self performTask];

}

-(id)init

{

if ((self = [super init]))

{

}

return self;

}

-(void)dealloc

{

[super dealloc];

}

@end


#import <Foundation/Foundation.h>

#import "BaseController.h"

@interface DerivedController : BaseController

-(void)performTask;

@end


#import "DerivedController.h"

@implementation DerivedController

-(void)performTask

{

NSLog(@"Inherited method loaded.");

}

-(id)init

{

if ((self = [super init])) { }

return self;

}

-(void)dealloc

{

[super dealloc];

}

@end

I would like to override the method performTask in a derived class and have the unchanged login method call the overridden method. This should be easy. What am I missing?

网友答案:

Just override performTask and the subclass instance will automatically use its own implementation when it receives the performTask message.

网友答案:

As mentioned by Chuck, just override the method, but pay attention to not call [super performTask] in your new implementation.

相关阅读:
Top