美文网首页
Objective-C 继承

Objective-C 继承

作者: CaptainRoy | 来源:发表于2018-07-23 16:52 被阅读0次
    • 继承于 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 继承

          本文链接:https://www.haomeiwen.com/subject/nwpjmftx.html