我正在阅读Objective-C 编程教程,我已经开始尝试为 XYZPerson 实现类工厂方法
一切看起来都很好,直到我尝试使用名为 XYZShoutingPerson 的子类实例化一个对象。我在里面收到以下错误main.m
使用“XYZPerson *”类型的表达式初始化“XYZShoutingPerson *”的不兼容指针类型
XYZPerson.h
#import <Foundation/Foundation.h>
@interface XYZPerson : NSObject
@property NSString *firstName;
@property NSString *lastName;
@property NSDate *dateOfBirth;
+ (XYZPerson *)person;
- (void)sayHello;
- (void)saySomething:(NSString *)greeting;
@end
XYZPerson.m
#import "XYZPerson.h"
@implementation XYZPerson
+ (id)person
{
return [[self alloc] init];
}
- (void)sayHello
{
[self saySomething:@"Hello, World!"];
}
- (void)saySomething:(NSString *)greeting
{
NSLog(@"%@", greeting);
}
@end
XYZ喊叫人.h
#import "XYZPerson.h"
@interface XYZShoutingPerson : XYZPerson
@end
XYZ喊叫人.m
#import "XYZShoutingPerson.h"
@implementation XYZShoutingPerson
- (void)saySomething:(NSString *)greeting
{
NSString *uppercaseGreeting = [greeting uppercaseString];
[super saySomething:uppercaseGreeting];
}
@end
主文件
#import <Foundation/Foundation.h>
#import "XYZPerson.h"
#import "XYZShoutingPerson.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
XYZPerson *newPerson = [XYZPerson person];
[newPerson sayHello];
XYZShoutingPerson *shoutingPerson = [XYZShoutingPerson person];
[shoutingPerson sayHello];
}
return 0;
}
任何帮助将非常感激。
谢谢!