我正在尝试覆盖 Objective-C 中的吸气剂。我有两个班级,First
并且Second
,例如:
First.h
:
@interface First : MashapeResponse {
NSString* message;
}
@property(readwrite, retain) NSString* message;
@end
First.m
:
#import "First.h"
@implementation First
@synthesize message;
@end
Second.h
:
#import "First.h"
@interface Second : First
-(NSString*) message;
@end
Second.m
:
#import "Second.h"
@implementation Second
-(NSString*) message {
return [NSString stringWithFormat:@"%@%@", @"Message is ", message];
}
@end
我正在尝试执行以下代码,但似乎Second
永远不会执行覆盖的方法。
First* first = [[First alloc] init];
[first setMessage:@"hello"];
NSLog(@"%@", [first message]); // "hello" expected
Second* second = (Second*) first;
NSLog(@"%@", [second message]); // "Message is hello" expected, but it's still "hello"