OutMan——Objective-C中成员变量的作用域、@property和@synthesize介绍和使用、构造方法和自定义构造方法

来源:互联网 发布:mac双系统卸载win7 编辑:程序博客网 时间:2024/04/30 07:35

成员变量的作用域

一、成员变量作用域:
@public:在任何地方都能直接访问对象的成员变量
@private:只能在当前类的对象方法中直接访问(@implementation中声明的成员变量默认是@private)
@protected:能在当前类及其子类的对象方法中直接访问(@interface中声明的成员变量默认是@protected)
@package:同一“体系内”(框架)可以访问,作用域介于@private和@public之间

二、成员变量的作用域分析:
        创建Person类(父类),Student类(子类)两个类,来分析成员变量的作用域
(1)Person类的设计
                                                                     Person.h文件

#import <Foundation/Foundation.h>@interface Person : NSObject{    @public    NSString *_name;   // 姓名    @private    int _age;   // 年龄    @protected    float _weight;   // 体重}// 姓名的setter和getter声明- (void)setName:(NSString *)name;- (NSString *)name;// 年龄的setter和getter声明- (void)setAge:(int)age;- (int)age;// 体重的setter和getter声明- (void)setWeight:(float)weight;- (float)weight;// run方法的声明- (void)run;@end

                                                                     Person.m文件

#import "Person.h"@implementation Person// 姓名的setter和getter实现- (void)setName:(NSString *)name{    _name = name;}- (NSString *)name{    return _name;}// 年龄的setter和getter实现- (void)setAge:(int)age{    _age = age;}- (int)age{    return _age;}// 体重的setter和getter实现- (void)setWeight:(float)weight{    _weight = weight;}- (float)weight{    return  _weight;}// run方法的实现- (void)run{    NSLog(@"Person-----名字:%@,年龄:%d,体重:%.2f", _name, _age, _weight);}@end

-设计注意:
1. 实现Person类的run方法时候,可以在run方法的实现中访问成员变量_name、_age、 _weight,说明作用域为@public、@private、@protected的成员变量可以在当前类的对象方法中直接访问

(2)Student类的设计
                                                                     Student.h文件

#import "Person.h"@interface Student : Person@end

                                                                     Student.m文件

#import "Student.h"@implementation Student// 重写父类中的run方法- (void)run{    NSLog(@"Student-----名字:%@,体重%.2f", _name, _weight);}@end

-设计注意:
1.作用域为@private的成员变量只能在当前类的对象方法中访问,子类的对象方法中不能访问
这里写图片描述

(3)main.m文件
这里写图片描述
-设计注意:
1.作用域为@public的成员变量,在main.m文件中可以通过对象->成员变量的方式来进行访问
这里写图片描述

@property和@synthesize的介绍和使用

一、@property和@synthesize
(1)@property
1. 用在@interface中
2. 用来自动生成setter和getter的声明
3. 例:用@property int age;
        就可以代替下面两行代码

// age的setter和getter的声明- (void)setAge:(int)age;- (int)age;

(2)@synthesize
1. 用在@implementation中
2. 用来自动生成setter和getter的实现
3. 例:用@synthesize age = _age;
        就可以代替下面代码

// age的setter和getter的实现- (void)setAge:(int)age{    _age = age;}- (int)age{    return _age;}

二、@synthesize的细节
(1)@synthesize age = _age;
        setter和getter实现会访问成员变量_age,如果成员变量_age不存在,就会自动生成一个@private类型的成员变量_age
(2)@synthesize age;
        setter和getter实现会访问成员变量age,如果成员变量age不存在,就会自动生成一个@private类型的成员变量age
(3)手动实现
        若手动实现了setter方法,编译器就只会自动生成getter方法(若成员变量不存在,编译器会自动生成不存在的成员变量)
        若手动实现了getter方法,编译器就只会自动生成setter方法(若成员变量不存在,编译器会自动生成不存在的成员变量)
        若同时手动实现了setter和getter方法,编译器就不会自动生成不存在的成员变量

三、@property的新特性
        自从Xcode4.4后,@property就独揽了@synthesize的功能,也就是说,@property可以同时生成setter和getter的声明和实现,默认情况下,setter和getter方法中的实现,会去访问下划线开头的成员变量

四、@property和@synthesize的使用分析
        设计一个Person类,来分析@property和@synthesize的使用
(1)Person类的设计
                                                                     Person.h文件

#import <Foundation/Foundation.h>@interface Person : NSObject{    int age;    int _age;}// age的setter和getter声明@property int age;// height的setter和getter声明@property float height;// @property新特性的使用@property NSString *name;- (void)run;@end

                                                                     Person.m文件

#import "Person.h"@implementation Person// age的setter和getter实现@synthesize age;//height的setter和getter实现@synthesize height = _height;- (void)run{    NSLog(@"_age---------->%d", _age);    NSLog(@"age----------->%d", age);    NSLog(@"身高:%.2f,名字:%@", _height, _name);}@end

(2)main.m文件
这里写图片描述
-从运行结果可以看出:
1. @synthesize age;
        setter和getter实现会访问成员变量age,如果成员变量age不存在,就会自动生成一个@private类型的成员变量age
2. @synthesize height = _height;
        setter和getter实现会访问成员变量_height,如果成员变量_height不存在,就会自动生成一个@private类型的成员变量_height
3. @property NSString * name;
        自从Xcode4.4后,@property就独揽了@synthesize的功能,也就是说,@property可以同时生成setter和getter的声明和实现,默认情况下,setter和getter方法中的实现,会去访问下划线开头的成员变量

构造方法和自定义构造方法

一、构造方法
1. 构造方法:用来初始化对象的方法,是个对象方法,以减号-开头
2. 重写构造方法的目的:为了让对象创建出来,成员变量就会有一些固定的值
3. 重写构造方法的注意点:
(1)先调用父类的构造方法([ super init ])
(2)再经行子类内部成员变量的初始化

二、构造方法的分析:
        要求:学生对象初始化完毕后,年龄就是10,学号就是1
(1)Student类的设计
                                                                     Student.h文件

#import <Foundation/Foundation.h>@interface Person : NSObject@property int age;   // 年龄@property int no;   // 学号@end

                                                                     Student.m文件

#import "Person.h"@implementation Person// 重写init方法- (instancetype)init{    // 1.一定要调用会super的init方法:初始化父类中声明的一些成员变量和其他属性    if (self = [super init])    {        // 2.如果对象初始化成功,才有必要进行接下来的初始化        _age = 10;        _no = 1;    }    // 3.返回一个已经初始化完毕的对象    return self;}@end

(2)main.m文件
这里写图片描述
-设计注意:
1. [super init]的作用:利用父类的init的方法,初始化父类中声明的一些成员变量和其他属性
2. self为什么要赋值为[super init]?简单来说是为了防止父类的初始化方法release掉了self指向的空间并重新alloc了一块空间,这时的话[super init]可能alloc失败,这时就不会再执行if中的语句
3. return self的作用:返回一个初始化完毕的对象

三、自定义构造方法
        自定义构造方法的规范:
1. 一定是对象方法,一定以减号-开头
2. 返回值一般是id类型
3. 方法名一般以initWith开头

四、自定义构造方法的分析
        创建Person类,要求:创建出来的人有不同的名称和年龄,利用之前的Person类创建Student类,要求:创建出来的学生有不同的姓名,年龄和学号
(1)Person类的设计
                                                                     Person.h文件

#import <Foundation/Foundation.h>@interface Person : NSObject@property NSString *name;   // 姓名@property int age;   // 年龄- (instancetype)initWithName:(NSString *)name andAge:(int)age;@end

                                                                     Person.m文件

#import "Person.h"@implementation Person- (instancetype)initWithName:(NSString *)name andAge :(int)age{    if (self = [super init])    {        _name = name;        _age = age;    }    return self;}@end

(2)Student类的设计
                                                                     Student.h文件

#import "Person.h"@interface Student : Person@property int no;   // 学号- (instancetype)initWithName:(NSString *)name andAge :(int)age andNo:(int)no;@end

                                                                     Student.m文件

#import "Student.h"@implementation Student- (instancetype)initWithName:(NSString *)name andAge:(int)age andNo:(int)no{    if ([super initWithName:name andAge:age])    {        _no = no;    }    return self;}@end

(3)main.m
这里写图片描述
-设计注意:
1.

- (instancetype)initWithName:(NSString *)name andAge:(int)age andNo:(int)no{    if ([super initWithName:name andAge:age])    {        _no = no;    }    return self;}

        这样写的好处是:父类的属性交给父类方法去处理,子类方法处理子类自己的属性

instancetype和id

一、instancetype和id的异同:
(1)相同点:
1. 都可以作为方法的返回类型
(2)不同点:
1. instancetype可以返回和方法所在类相同类型的对象,id只能返回未知的对象
2. instancetype只能作为返回值,不能像id那样作为参数
3. id可以作为方法或函数的返回值类型,参数类型,也能用来定义变量

二、instancetype对比id的好处:
        instancetype可以智能的帮我们判断赋值对象类型和指向对象的指针类型是否一致(能精确的限制返回值的具体类型)


                                        —— 如果您有不同的见解,请随时指出,期待与您的交流!——


0 0
原创粉丝点击