Objective-c中的类

2017.7.29

Objective-c中的类 set和get方法

Student.h
#import "Person.h"

@interface Student : Person
{
    NSString *_subject;
    NSString *_degree;
}
  //自定义初始化方法
- (instancetype)initWithSubject:(NSString *)subject;
- (instancetype)initWithDegree:(NSString *)degree;
- (instancetype)initWithSubject:(NSString *)subject degree:(NSString *)degree;
 
- (void) setSubject:(NSString *)subject;
- (NSString *)subject;

- (void)setDegree:(NSString *)degree;
- (NSString *)degree;
@end
Student.m
#import "Student.h"m
@implementation Student

- (instancetype)initWithSubject:(NSString *)subject{
   return [self initWithDegree:@"junior"];
}
//初始化方法在生命周期内只能调用一次
- (instancetype)initWithSubject:(NSString *)subject degree:(NSString *)degree{
    self = [super initWithName:_name age:_age];
    if (self) {
        _subject = subject;
        _degree = degree;
        
    }
    return self;
}

- (void) setSubject:(NSString *)subject{
    _subject = subject;
}
- (NSString *)subject{
    return _subject;
}

- (void)setDegree:(NSString *)degree{
    _degree = degree;
}
- (NSString *)degree {
    return _degree;
}
- (void)study{
    //父类继承过来的方法实现
    [super study];
    NSLog(@"我是%@,性别%@----,专业%@,%@",_name,_gender,_subject,_degree);
}

@end
main.m

    Person *person = [[Person alloc] init];
    [person setName:@"鹿晗"];
    [person setAge:21];
    [person name];
    [person age];
    //本质上等于上面的setName方法
    person.name = @"lucy";
    person.age = 14;
    NSLog(@"name:%@,age:%ld",person.name,person.age);

    Person *p = [Person personWithName:@"Tony" age:0];
    NSLog(@"姓名:%@,age:%ld",p.name,p.age);
    Student *stu = [[Student alloc] init];
    stu.name = @"小明";
    stu.subject = @"iOS";
    stu.degree = @"Senior";
    stu.gender = @"man";
    stu.age = 11;
    //study是在父类中实现的,study继承过来,可以使用
    [stu study ];

    Student *st = [[Student alloc] initWithSubject:@"iOS" degree:@"本科"];
    st.name = @"hfy";
    st.age = 12;
    NSLog(@"subject:%@,degree:%@",st.subject,st.degree);
    NSLog(@"name:%@,age:%ld",st.name,st.age);
Person.h
#import 
@interface Person : NSObject
{
    NSString *_name;
    NSInteger _age;
    NSString *_gender;
    
}
//便利构造器

+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age;
- (instancetype)initWithName:(NSString *)name;
- (instancetype)initWithAge:(NSInteger)age;
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;

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

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

- (void)setGender:(NSString *)gender;
- (NSString *)gender;
- (void)study;

@end

Person.m
#import "Person.h"

@implementation Person
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age{
    return [[Person alloc] initWithName:name age:age];
}
- (instancetype)initWithAge:(NSInteger)age{
    return [self initWithName:@"丽丽" age:age];
}
- (instancetype)initWithName:(NSString *)name{
   
    return [self initWithName:name age:0];
}
//指派初始化方法

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age{
    //调用父类的初始化方法
    self = [super init];
    if (self) {
   //这样的初始化方法可以改变值,与系统自带的初始化方法不同
        _name = name;
        _age = age;
    }
    return self;
}
//系统自带的初始化方法

- (instancetype)init
{
    self = [super init];
    if (self) {
     //这两个值是固定的,无法改变
        _name = @"lily";
        _age = 43;
        
    }
    return self;
}
//set方法
- (void)setName:(NSString *)name {
    _name = name;
}
//get方法
- (NSString *)name{
    return _name;
}
- (void)setAge:(NSInteger)age{
    _age = age;
}
- (NSInteger)age{
    return _age;
}

- (void)setGender:(NSString *)gender{
    _gender = gender;
}
- (NSString *)gender{
    return _gender;
}
- (void)study{
        NSLog(@"我是%@,age:%ld",_name,_age);
    }
@end

你可能感兴趣的:(Objective-c中的类)