OC 繼承

  • 新建一個 Student 類繼承于 Person 類
#import "Person.h"

extern NSString * const Education;

@interface Student : Person

@property(nonatomic,copy)NSString *homeTown; // 家鄉(xiāng)
@property(nonatomic,assign)NSUInteger classNum; // 班級
@property(nonatomic,copy)NSString *country; // 國籍

// 設(shè)置家鄉(xiāng) 班級 國籍
-(void)setHomeTown:(NSString *)homeTown andClassNum:(NSUInteger)classNum andCountry:(NSString *)country;

// 打印學(xué)生信息
-(void)introduce;

// 打印教育經(jīng)歷
-(void)printEduation:(NSString *)edcation;

// 打印職業(yè)規(guī)劃
-(void)printOccuptionPlan:(EmployeeType)employeeType;

@end
#import "Student.h"

NSString * const Education = @"本科";

@implementation Student

-(void)setHomeTown:(NSString *)homeTown andClassNum:(NSUInteger)classNum andCountry:(NSString *)country
{
    _homeTown = [homeTown copy];
    _classNum = classNum;
    _country = [country copy];
}

-(void)introduce
{
    NSLog(@"姓名: %@, 性別: %@, 年齡: %lu, 班級: %lu, 家鄉(xiāng): %@, 國籍: %@",
          self.name,self.sex,(unsigned long)self.age,(unsigned long)self.classNum,self.homeTown,self.country);
}

-(void)printEduation:(NSString *)edcation
{
    if (!edcation) {
        NSLog(@"%@ 的學(xué)歷是: %@",self.name,Education);
    } else {
        NSLog(@"%@ 的學(xué)歷是: %@",self.name,edcation);
    }
}

-(void)printOccuptionPlan:(EmployeeType)employeeType
{
    switch (employeeType) {
        case EmployeeTypeEngineer:
            NSLog(@"%@ 想成為 工程師",self.name);
            break;
        case EmployeeTypeDesigner:
            NSLog(@"%@ 想成為 設(shè)計師",self.name);
            break;
        case EmployeeTypeFinance:
            NSLog(@"%@ 想成為 財務(wù)",self.name);
            break;
        default:
            NSLog(@"%@ 還不清楚",self.name);
            break;
    }
}


@end
Student *lily = [[Student alloc] init]; // -[Person init]
lily.name = @"lily";
lily.age = 28;
lily.sex = @"女";
[lily setHomeTown:@"湖北" andClassNum:2 andCountry:Nationality];
[lily introduce]; // 姓名: lily, 性別: 女, 年齡: 28, 班級: 2, 家鄉(xiāng): 湖北, 國籍: 中國
[lily printEduation:NULL]; // lily 的學(xué)歷是: 本科
[lily printOccuptionPlan:EmployeeTypeFinance]; // lily 想成為 財務(wù)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 思考:當(dāng)多個類,擁有部分相同的成員變量時,怎么辦? 1.復(fù)制-粘貼 缺點(diǎn):當(dāng)需求有任何改動時,需要改動的代碼量很大...
    螢火蟲兒飛_閱讀 1,039評論 0 0
  • 在實(shí)際的開發(fā)過程中,對于界面相似度很高的情況下,使用繼承可以節(jié)省很多代碼設(shè)置,涉及到的額外的東西,可以再子類中重寫...
    忘惘的小風(fēng)扇閱讀 1,234評論 1 1
  • Scala與Java的關(guān)系 Scala與Java的關(guān)系是非常緊密的?。?因為Scala是基于Java虛擬機(jī),也就是...
    燈火gg閱讀 3,610評論 1 24
  • 1.感恩春光融融的一天,特別的美好! 2.感恩我和大女約定早上一起爬完寶嶺再上學(xué),咱倆起的特別的早! 3.感恩走到...
    旖旎漫步閱讀 246評論 0 1
  • 單例設(shè)計模式從字面意思上來說,就是一個類在系統(tǒng)運(yùn)行時,只創(chuàng)建一個實(shí)例??梢杂糜谛枰欢啻握{(diào)用的或者多次使用的資源中...
    Originalee閱讀 371評論 0 10

友情鏈接更多精彩內(nèi)容