#import <Foundation/Foundation.h> #import "Person.h" #import "Student.h" #import "CollegeStudent.h" int main(int argc, const char * argv[]) { @autoreleasepool { //Person Person *per1 = [[Person alloc] initWithName:@"小彩旗" sex:@"女"age:16]; [per1 eat]; [per1 sayHi]; //Student Student *stu1 = [[Student alloc] initWithName:@"大红旗" sex:@"男" age:18]; [stu1 sleep]; [stu1 sayHi]; [stu1 setName:@"李刚"]; [stu1 setSex:@"真男"]; NSLog(@"%@",[stu1 name]); //点语法: 相当于代替中括号的方法调用,若没有 set 方法则不可使用点语法 stu1.age = 20; //等价于[stu1 setAge:20] NSLog(@"%ld",stu1.age); //等价于[stu1 age]; //创建 collgestudent //子类的子类可以获取父类的父类的方法和实例变量(继承具有传递性,且为单向传递) CollegeStudent *zhangsan = [[CollegeStudent alloc] initWithName:@"zhangsan" sex:@"male" num:@"23" score:2.3]; CollegeStudent *lisi = [[CollegeStudent alloc] initWithName:@"lisi " sex:@"female" num:@"35" score:35.4 major:@"petroleum" school:@"chengoil"]; //对于 sayhi 方法,先从当前类中去找实现,如果没有再到父类中去找,有的话执行;若没有,继续向父类的父类查找有无该方法的实现,一直到找到为止,然后实现. [lisi sayHi]; //指定初始化方法,,完全初始化 CollegeStudent *aa = [[CollegeStudent alloc]initWithName:@"mmmmmmmmm" num:@"11111111111"]; [aa sayHi]; //便利构造器 Person *per = [Person personWithName:@"ooooo" sex:@"male" age:20]; } return 0; }
<Student.h>
#import "Person.h" // : 表示继承 // :Person 表示继承于 Person, 就意味着, Student 获取Person 中所有的实例变量和方法 @interface Student : Person { NSString *_num; CGFloat _score; } -(id)initWithName:(NSString *)name sex:(NSString *)sex num:(NSString *)num score:(CGFloat)score; //setter and getter -(void)setNum:(NSString *)num; -(NSString *) num; -(void)setScore:(CGFloat)score; -(CGFloat)score; -(void)takeClass; -(void)exam; @end
<Student.m>
#import "Student.h" @implementation Student -(id)initWithName:(NSString *)name sex:(NSString *)sex num:(NSString *)num score:(CGFloat)score { if ([super init]) { _name = name; _sex = sex; _num =num; _score = score; } return self; } //setter and getter -(void)setNum:(NSString *)num { _num = num; } -(NSString *) num { return _num; } -(void)setScore:(CGFloat)score { _score = score; } -(CGFloat)score { return _score; } -(void)takeClass { NSLog(@"我是%@,我再上课",[self name]); //如果想在当前调用实例方法使用 self,self 代表当前类的对象 } -(void)exam { NSLog(@"考试"); } @end
<CollegeStudent.h>
#import "Student.h" @interface CollegeStudent : Student { NSString *_major; NSString *_school; } -(id)initWithName:(NSString *)name sex:(NSString *)sex num:(NSString *)num score:(CGFloat)score major:(NSString *)major school:(NSString *)school; -(void)setMajor:(NSString *)major; -(NSString *)major; -(void)setSchool:(NSString *)school; -(NSString *)school; -(void)findObject; //补考 -(void)makeUpExam; //重新实现 sayHi -(void)sayHi; //指定初始化方法 -(id)initWithName:(NSString *)name num:(NSString *)num; -(id)initWithName:(NSString *)name school:(NSString *)school major:(NSString *)major; @end
<CollegeStudent.m>
#import "CollegeStudent.h" @implementation CollegeStudent -(id)initWithName:(NSString *)name sex:(NSString *)sex num:(NSString *)num score:(CGFloat)score major:(NSString *)major school:(NSString *)school //{ // if ([super init]) { // _name = name; // _sex = sex; // _num = num; // _score = score; // _major = major; // _school = school; // } // return self; //} // 这样写就可以不用写_name = name;_sex = sex;_num = num;_score = score;了 { //super 编译指令,不代表父类对象,调用父类方法的实现!!!!!!!!! if([super initWithName:name sex:sex num:num score:score]){ _major = major; _school = school; } return self; } -(void)setMajor:(NSString *)major { _major = major; } -(NSString *)major { return _major; } -(void)setSchool:(NSString *)school { _school = school; } -(NSString *)school { return _school; } -(void)findObject { NSLog(@" 找对象"); } //补考 -(void)makeUpExam { NSLog(@"补考"); } -(void)sayHi { NSLog(@"%@,%@,%@,%.2f,%@,%@",_name,_sex,_num,_score,_major,_school); } //指定初始化方法 -(id)initWithName:(NSString *)name num:(NSString *)num { return [self initWithName:name sex:nil num:num score:0 major:nil school:nil]; } -(id)initWithName:(NSString *)name school:(NSString *)school major:(NSString *)major { return [self initWithName:name sex:nil num:nil score:0 major:major school:school]; } @end
<Person.h>
#import <Foundation/Foundation.h> @interface Person : NSObject { NSString *_name; NSString *_sex; NSInteger _age; } //自定义初始化方法 -(id)initWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger )age; //便利构造器 +(Person *)personWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age; -(void)setName:(NSString *)name; -(NSString *)name; -(void)setAge:(NSInteger)age; -(NSInteger)age; -(void)setSex:(NSString *)sex; -(NSString *)sex; -(void)sleep; -(void)eat; -(void)playBean; -(void)sayHi; @end
<Person.m>
#import "Person.h" @implementation Person //自定义初始化方法 -(id)initWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger )age { self = [super init]; if (self) { _name = name; _sex = sex; _age = age; } return self; } //便利构造器 //以 + 开头 表示类方法 , 返回值类型为当前类, 方法名以当前类名小写开头 + With(W 大写) + 各个参数 //其实就是将创建对象的过程初始化的过程,封装到一个方法里面 +(Person *)personWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age { return [[Person alloc ] initWithName:name sex:sex age:age]; } -(void)setName:(NSString *)name { _name = name; } -(NSString *)name { return _name; } -(void)setAge:(NSInteger)age { _age = age; } -(NSInteger)age { return _age; } -(void)setSex:(NSString *)sex { _sex = sex; } -(NSString *)sex { return _sex; } -(void)sleep { NSLog(@"睡觉"); } -(void)eat { NSLog(@" 吃饭"); } -(void)playBean { NSLog(@" 打豆豆"); } -(void)sayHi { NSLog(@"name %@,sex %@,age %ld",_name ,_sex,_age); } @end