Obj-c3[继承,构造便利器]

main.m

#import <Foundation/Foundation.h>

#import "Person.h"

#import "Student.h"

#import "COllegeStudent.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//继承
        
//        Person * person = [[Person alloc] init];
//        [person sayHi];
//        Student * student = [[Student alloc] init];
//        [student sayHi];
//        
//        COllegeStudent * p = [[COllegeStudent alloc]init];
//        [p sayHi];
//        
//        Student * student1 = [[Student alloc] initWithName:@"张三" sex:@"男" age:19 number:10005 score:89];
//        [student1 sayHi];
//        COllegeStudent * p = [[COllegeStudent alloc] initWithName:@"战神嘎哈" sex:@"男" age:55 number:101111 score:99.99 major:@"计算机" academy:@"计算机世界"];
//        [p sayHi];
//        COllegeStudent * p1 = [[COllegeStudent alloc] initWithName:@"Tom" sex:@"M" age:19 number:1005];
//        [p1 sayHi];
        
        Person * p1 = [Person personWithName:@"张三"];
        [p1 sayHi];
        
        
//        Student * student1 = [[Student alloc] init];
//        [student1 setName:@"张三"];
//        [student1 setSex:@"男"];
//        [student1 setAge:25];
//        [student1 setNumber:10001];
//        [student1 setScore:88.5];
//        [student1 sayHi];
//        [student1 study];
        
//        COllegeStudent * collageStudent1 = [[COllegeStudent alloc] init];
//        [collageStudent1 setName:@"杨紫"];
//        [collageStudent1 setSex:@"女"];
//        [collageStudent1 setAge:23];
//        [collageStudent1 setNumber:100012];
//        [collageStudent1 setScore:99.9];
//        [collageStudent1 setMajor:@"表演"];
//        [collageStudent1 setAcademy:@"北影"];
//        [collageStudent1 sayHi];
        
        
//        Dog * dog = [[Dog alloc] init];
//        [dog call];
//        
//        [Dog testMnethod];
//        
//        Cat * cat = [[Cat alloc] init];
//            NSLog(@"%@",cat);
//        [cat call];
//        
//        Cat * cat1 =[[Cat alloc] init];
//            NSLog(@"%@",cat1);
//        [cat1 call];
//            
    }
    return 0;
}
------------Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    NSString * _name;
    NSString * _sex;
    NSInteger  _age;
}

- (void)sayHi;

- (id)init;
//便利构造器
//+ 号方法,返回值是id,方法名一类名打头
//+ (id)personWithName:(NSString *)name;

- (id)initWithName:(NSString *)name;
+ (id)personWithName:(NSString *)name;

- (id)initWithSex:(NSString *)sex;
+ (id)personWithSex:(NSString *)sex;

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex;
+ (id)personWithName:(NSString *)name
                 sex:(NSString *)sex;

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age;
+ (id)personWithName:(NSString *)name
                 sex:(NSString *)sex
                 age:(NSInteger)age;

- (void)setName:(NSString *)name;
- (NSString *)name;

- (void)setSex:(NSString *)sex;
- (NSString *)sex;

- (void)setAge:(NSInteger)age;
- (NSInteger)age;

@end

----------Person.m


#import "Person.h"


@implementation Person

- (void)sayHi{
    NSLog(@"姓名:%@,年龄:%ld,性别%@",_name, _age, _sex);
}
- (id)init{
    self = [super init];
    if (self) {
        _name = @"张志义";
        _age = 18;
        _sex = @"男";
    }
//    _name = @"张志义";
//    _age = 18;
//    _sex = @"男";

    return self;

}

- (id)initWithName:(NSString *)name{
//    self = [super init];
//    if (self) {
//        _name = name;
//    }
//    return self;

    return [self initWithName:name sex:nil age:0];
}

//便利构造器
//+ 号方法,返回值是id,方法名一类名打头
+ (id)personWithName:(NSString *)name
{
    Person * p = [[Person alloc] initWithName:name];
    return p;
}

- (id)initWithSex:(NSString *)sex{
//    self = [super init];
//    if (self) {
//        _sex = sex;
//    }
//    return self;
    return [self initWithName:nil sex:sex age:0];
}
+ (id)personWithSex:(NSString *)sex{
    Person * p = [[Person alloc] initWithSex:sex];
    return p;
}

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex{
//    self = [super init];
//    if (self) {
//        _name = name;
//        _sex = sex;
//    }
//    return self;
    return [self initWithName:name sex:sex age:0];
}
+ (id)personWithName:(NSString *)name
                 sex:(NSString *)sex{
    Person * p = [[Person alloc] initWithName:name sex:sex];
    return p;
}

//指定初始化方法,无论调用哪个初始化方法,最终调用到的初始化方法,称为是指定初始化方法,一个类只有一个指定初始化方法
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age{
    self = [super init];
    if (self) {
        _name = name;
        _sex = sex;
        _age = age;
    }
    return self;
}
+ (id)personWithName:(NSString *)name
                 sex:(NSString *)sex
                 age:(NSInteger)age{
    Person * p = [[Person alloc] initWithName:name sex:sex age:age];
    return p;
}


- (void)setName:(NSString *)name{
    _name = name;
}
- (NSString *)name{
    return _name;
}

- (void)setSex:(NSString *)sex{
    _sex = sex;
}
- (NSString *)sex{
    return _sex;
}

- (void)setAge:(NSInteger)age{
    _age = age;}
- (NSInteger)age{
    return _age;
}

@end


---------------------Student.h

#import "Person.h"

@interface Student : Person
{
    NSInteger _number;
    CGFloat   _score;
}

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number;
+ (id)studentWithName:(NSString *)name
                  sex:(NSString *)sex
                  age:(NSInteger)age
               number:(NSInteger)number;

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score;

+ (id)studentWithName:(NSString *)name
                  sex:(NSString *)sex
                  age:(NSInteger)age
               number:(NSInteger)number
                score:(CGFloat)score;

- (void)setNumber:(NSInteger)number;
- (NSInteger)number;

- (void)setScore:(CGFloat)score;
- (CGFloat)score;

- (id)init;

- (void)study;

@end

----------------Student.m

//重写父类方法,调用的时候就会执行子类实现,如果子类没有重写,会逐级向上查找,找到即执行
- (void)sayHi{
    NSLog(@"I am a student, name is %@, sex is %@, age is %ld,number is %ld, score is %.2f",_name, _sex, _age, _number, _score);
}

- (id)init{
    
        self = [super init];
        if (self) {
                _number = 100002;
                _score = 86.6;
        }
//    _name = @"王武";
//    _age = 19;
//    _sex = @"男";
//    _number = 100002;
//    _score = 86.6;
    return self;
}




- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number{
    return [self initWithName:name sex:sex age:age number:number score:0];
}

+ (id)studentWithName:(NSString *)name
                  sex:(NSString *)sex
                  age:(NSInteger)age
               number:(NSInteger)number{
    
    Student * p = [[Student alloc] initWithName:name sex:sex age:age number:number];
    return  p;
    
}

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score{
//    self = [super self];
//    if (self) {
//        _name = name;
//        _sex = sex;
//        _age = age;
//        _number = number;
//        _score = score;
//    }
//    return self;
    self = [super initWithName:name sex:sex age:age];
    if (self) {
        _number = number;
        _score = score;
    }
    return self;
}

+ (id)studentWithName:(NSString *)name
                  sex:(NSString *)sex
                  age:(NSInteger)age
               number:(NSInteger)number
                score:(CGFloat)score{

    Student * p = [[Student alloc] initWithName:name sex:sex age:age number:number score:score];
    return  p;
    
}
- (void)study{
    NSLog(@"学习");
}

- (void)setNumber:(NSInteger)number{
    _number = number;
}
- (NSInteger)number{
    return _number;
}

- (void)setScore:(CGFloat)score{
    _score = score;
}
- (CGFloat)score{
    return _score;
}

@end


------------COllegeStudent.h

#import "Student.h"

@interface COllegeStudent : Student
{
    NSString * _major;//专业
    NSString * _academy;//学院
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex;

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number;
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score
             major:(NSString *)major;
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score
             major:(NSString *)major
           academy:(NSString *)academy;


- (id)init;
- (void)setMajor:(NSString *)major;
- (NSString *)major;

- (void)setAcademy:(NSString *)academy;
- (NSString *)academy;
@end

------------COllegeStudent.m


#import "COllegeStudent.h"

@implementation COllegeStudent

//重写父类方法,调用的时候就会执行子类实现,如果子类没有重写,会逐级向上查找,找到即执行
- (void)sayHi{
    NSLog(@"I am a student, name is %@, sex is %@, age is %ld,number is %ld, score is %.2f, major is %@, academy is %@",_name, _sex, _age, _number, _score, _major, _academy);
}

- (id)init{
    
    
    self = [super init];
    if (self) {
        _major = @"软件开发";
        _academy = @"软件工程";
    }
//    _name = @"王武";
//    _age = 19;
//    _sex = @"男";
//    _number = 100002;
//    _score = 86.6;
//    _major = @"软件开发";
//    _academy = @"软件工程";
    return self;
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex{
    return [self initWithName:name sex:sex age:0 number:0 score:0 major:nil academy:nil];
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number{
    return [self initWithName:name sex:sex age:age number:number score:0 major:nil academy:nil];
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score
             major:(NSString *)major{
    return [self initWithName:name sex:sex age:age number:number score:score major:major academy:nil];
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score
             major:(NSString *)major
           academy:(NSString *)academy{
    self = [super initWithName:name sex:sex age:age number:number score:score];
    if (self) {
        _major = major;
        _academy = academy;
    }
    return self;
}


- (void)setMajor:(NSString *)major{
    _major = major;
}
- (NSString *)major{
    return _major;
}

- (void)setAcademy:(NSString *)academy{
    _academy = academy;
}
- (NSString *)academy{
    return _academy;
}
@end

你可能感兴趣的:(自定义便利器)