2、类和对象-实例变量可见度

来源:互联网 发布:中国什么发生战争 知乎 编辑:程序博客网 时间:2024/06/15 19:19

2、类和对象-实例变量可见度


Person.h

#import <Foundation/Foundation.h>@interface Person : NSObject{    /** 实例变量可见度分3种:     *  public : 公有的,可见度最高,外部、内部、子类均可访问,缺点不安全     *  protected : 默认的,只有内部和子类可以访问,最常用     *  private : 私有的,可见度最低,只有自己能使用     */    @public    NSString *_address;    NSString *_hobby;    @protected    NSString *_name;    NSInteger _age;    @private    NSString *_gender;}// 声明赋值的方法- (void)setName:(NSString *)newName;- (void)setAge:(NSInteger)newAge;- (void)setGender:(NSString *)newGender;// 声明取值方法- (NSString *)name;- (NSInteger)age;- (NSString *)gender;// 声明有2\3个参数的方法- (void)setName:(NSString *)name age:(NSInteger)age;- (void)replaceName:(NSString *)name age:(NSInteger)age gender:(NSString *)gender;// 声明自定义初始化方法- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;// 声明一个参数最全的自定义初始化方法// 通常选择参数最多的方法作为:指定初始化方法// 指定初始化方法:无论使用那一个初始化方法创建对象,都会执行此方法- (instancetype)initWithName:(NSString *)name                         age:(NSInteger)age                      gender:(NSString *)gender                     address:(NSString *)address                       hobby:(NSString *)hobby;@end

Person.m

#import "Person.h"@implementation Person// 实现给_name实例变量赋值的方法- (void)setName:(NSString *)newName{    // 将参数的值,赋给执行此方法的对象的实例变量    self->_name = newName;}- (void)setAge:(NSInteger)newAge{    // 在方法内部,可以直接写实例变量的变量名,‘self->’可省略    _age = newAge;}- (void)setGender:(NSString *)newGender{    _gender = newGender;}// 实现取值的方法- (NSString *)name{    return _name;  // or return self->_name;}- (NSInteger)age{    return _age;}- (NSString *)gender{    return _gender;}// 实现有2、3个参数的方法- (void)setName:(NSString *)name age:(NSInteger)age{    _name = name;    _age = age;}- (void)replaceName:(NSString *)name age:(NSInteger)age gender:(NSString *)gender{    _name = name;    _age = age;    _gender = gender;}// 重写父类方法的实现- (id)init{    _age = 19;    return self;}// 实现自定义初始化方法- (instancetype)initWithName:(NSString *)name age:(NSInteger)age{    _name = name;    _age = age;    return self;}- (instancetype)initWithName:(NSString *)name                         age:(NSInteger)age                      gender:(NSString *)gender                     address:(NSString *)address                       hobby:(NSString *)hobby;{    _name = name;    _age = age;    _gender = gender;    _address = address;    _hobby = hobby;    return self;}@end

Mother.h

#import <Foundation/Foundation.h>@interface Mother : NSObject@end

Mother.m

#import "Mother.h"@implementation Mother@end

Father.h

#import <Foundation/Foundation.h>@interface Father : NSObject@end

Father.m

#import "Father.h"@implementation Father@end

Child.h

#import <Foundation/Foundation.h>@interface Child : NSObject{    NSString *_name;}- (void)setName:(NSString *)name;- (NSString *)name;@end

Child.m

#import "Child.h"@implementation Child- (void)setName:(NSString *)name{    _name = name;}- (NSString *)name{    return _name;}@end

Family.h

#import <Foundation/Foundation.h>#import "Father.h"#import "Mother.h"#import "Child.h"@interface Family : NSObject{    Father *_father;    Mother *_mother;    Child *_child;}- (void)setFather:(Father *)father;- (Father *)father;- (void)setMother:(Mother *)mother;- (Mother *)mother;- (void)setChild:(Child *)child;- (Child *)child;@end

Family.m

#import "Family.h"@implementation Family- (void)setFather:(Father *)father{    _father = father;}- (Father *)father{    return _father;}- (void)setMother:(Mother *)mother{    _mother = mother;}- (Mother *)mother{    return _mother;}- (void)setChild:(Child *)child{    _child = child;}- (Child *)child{    return _child;}@end

main.m

#import <Foundation/Foundation.h>#import "Person.h"#import "Family.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        // OC中所有对象都要加 *        // 创建对象        Person *p = [[Person alloc] init];        // 通过方法设置受保护的实例变量的值        [p setName:@"laoda"];        [p setAge:19];        [p setGender:@"m"];        // 通过方法取出受保护的实例变量的值        NSString *n = [p name];        NSInteger a = [p age];        NSString *g = [p gender];        NSLog(@"%@ %ld %@", n, a, g);        Person *p1 = [[Person alloc] init];        [p1 setName:@"Emoji"];        // 创建对象        Person *p2 = [[Person alloc] init];                NSLog(@"%ld", [p2 age]);        // 使用自定义初始化方法创建对象        Person *p3 = [[Person alloc] initWithName:@"��" age:10];        // 创建Family对象        Family *family = [[Family alloc] init];        Father *f = [[Father alloc] init];        [family setFather:f];        Mother *m = [[Mother alloc] init];        [family setMother:m];        Child *c = [[Child alloc] init];        [c setName:@"马小桃"];        [family setChild:c];        // 获取name,要一层一层查找        NSLog(@"%@", [[family child] name]);    }    return 0;}
0 0
原创粉丝点击