Objective-C 继承

  • 继承于 Person 类的 Teacher 类
#import "Person.h"

@interface Teacher : Person

@property(nonatomic,readonly,copy)NSString *subject; // 科目

-(void)setSubject:(NSString *)subject;

-(void)teaching;

@end
#import "Teacher.h"

@implementation Teacher

-(void)setSubject:(NSString *)subject
{
    _subject = [subject copy];
}

-(void)teaching
{
    NSLog(@"开始上课,我教 %@",_subject);
}

@end
Teacher *lily = [[Teacher alloc] initWithName:@"lily" age:32 gender:GenderWoman];
[lily setSubject:@"语文"];
[lily introduce]; // 自我介绍,我的名字是 : lily,年龄 : 32,性别 : 2
[lily teaching]; // 开始上课,我教 语文

你可能感兴趣的:(Objective-C 继承)