OC|OC 继承

  • 新建一个 Student 类继承于 Person 类
#import "Person.h"extern NSString * const Education; @interface Student : Person@property(nonatomic,copy)NSString *homeTown; // 家乡 @property(nonatomic,assign)NSUInteger classNum; // 班级 @property(nonatomic,copy)NSString *country; // 国籍// 设置家乡 班级 国籍 -(void)setHomeTown:(NSString *)homeTown andClassNum:(NSUInteger)classNum andCountry:(NSString *)country; // 打印学生信息 -(void)introduce; // 打印教育经历 -(void)printEduation:(NSString *)edcation; // 打印职业规划 -(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, 家乡: %@, 国籍: %@", self.name,self.sex,(unsigned long)self.age,(unsigned long)self.classNum,self.homeTown,self.country); }-(void)printEduation:(NSString *)edcation { if (!edcation) { NSLog(@"%@ 的学历是: %@",self.name,Education); } else { NSLog(@"%@ 的学历是: %@",self.name,edcation); } }-(void)printOccuptionPlan:(EmployeeType)employeeType { switch (employeeType) { case EmployeeTypeEngineer: NSLog(@"%@ 想成为 工程师",self.name); break; case EmployeeTypeDesigner: NSLog(@"%@ 想成为 设计师",self.name); break; case EmployeeTypeFinance: NSLog(@"%@ 想成为 财务",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, 家乡: 湖北, 国籍: 中国 [lily printEduation:NULL]; // lily 的学历是: 本科 [lily printOccuptionPlan:EmployeeTypeFinance]; // lily 想成为 财务

    推荐阅读