实例变量可见度及方法、自定义初始化方法、实例化方法、类方法、便利构造器

来源:互联网 发布:水果店管理系统源码 编辑:程序博客网 时间:2024/06/13 01:20

实例变量:已经初始化过后的变量,所有指针类型的变量

成员变量:实例变量的一种,属于实例变量

对象:一类的指针变量(实例变量)


1. 实例变量可见度及方法

成员变量可见度有3种:

1) @public 类的内部和外部都可以访问,@public 暴露的类的内部细节,不能满足封装要求

2) @protected 受保护的. 外部不能访问, 内部可以访问

3) @private 私有的. 外部不能访问, 内部可以访问, 但他的子类不能访问

4) 默认是@protected 受保护的

例子:

Person.h文件
#import <Foundation/Foundation.h>@interface Person : NSObject{    // 成员变量可见度有3种:    @public    NSString *_name;    NSString *_sex;        @protected    NSString *_hobby;        @private    NSInteger _age;}// 赋值方法 setter// 1. set开头// 2. 无返回值// 3. OC 中只能带一个返回值- (void)setHobby:(NSString *)hobby;- (void)setAge:(NSInteger)age;// 取值方法 getter// 1. 带一个返回值的无参数的方法- (NSString *)hobby;- (NSInteger)age;- (void)info;// 多个参数的赋值方法- (void)setName:(NSString *)name            age:(NSInteger)age            sex:(NSString *)sex          hobby:(NSString *)hobby;@end
Person.m文件
#import "Person.h"@implementation Person// 赋值方法实现 - (void)setHobby:(NSString *)hobby{    _hobby = hobby;}- (void)setAge:(NSInteger)age{    _age = age;}// 取值方法实现- (NSString *)hobby{    return _hobby;}- (NSInteger)age{    return _age;}// 多个参数的赋值方法实现 - (void)setName:(NSString *)name age:(NSInteger)age sex:(NSString *)sex hobby:(NSString *)hobby{    _name = name;    _age = age;    _sex = sex;    _hobby = hobby;}- (void)info{    NSLog(@"name : %@, sex : %@, age : %lu, hobby : %@", _name, _sex, _age, _hobby);}@end
main 中

#import <Foundation/Foundation.h>#import "Person.h"#import "Student.h"int main(int argc, const char * argv[]) {    @autoreleasepool {                Person *person1 = [[Person alloc] init];        person1 -> _name = @"Tom";        person1 -> _sex = @"male";                // 方法        // 对于@protected修饰的实例变量, 外部不能直接访问, 提供两个方法, 赋值方法, 取值方法        // 赋值方法使用        [person1 setHobby:@"play"];        [person1 setAge:23];        // 取值方法使用          NSLog(@"hobby : %@", [person1 hobby]);        NSLog(@"age : %lu", [person1 age]);                [person1 info];                // 多个参数的赋值方法实现        [person1 setName:@"Jane" age:21 sex:@"female" hobby:@"read"];        [person1 info];    }    return 0;}
打印结果:

hobby : play

age : 23

name : Tom, sex : male, age : 23, hobby : play

name : Jane, sex : female, age : 21, hobby : read

2. 自定义初始化方法

1) 分配内存

2) 初始化

3) 成员变量赋值

例子:

Student.h文件

#import <Foundation/Foundation.h>@interface Student : NSObject{    NSString *_name;    NSString *_sex;    NSInteger _number;}// 自定义初始化方法// 1. - 开头// 2. 返回值类型 id(对象)// 3. initWith开头- (id)initWithName:(NSString *)name number:(NSInteger)number sex:(NSString *)sex;@end

Student.m文件
#import "Student.h"@implementation Student- (void)info{    NSLog(@"name : %@, sex : %@, number: %lu", _name, _sex, _number);}// 自定义初始化方法实现- (id)initWithName:(NSString *)name number:(NSInteger)number sex:(NSString *)sex{    _name = name;    _number = number;    _sex = sex;    return self;}@end
main 中
#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        Student *stu2 = [[Student alloc] initWithName:@"Lily" number:34 sex:@"female"];        [stu2 info];    }    return 0;}
打印结果:

name : Lily, sex : female, number: 34


3. 实例化方法( - 方法)

创建对象再调用方法

id:任意类型的指针变量

instancetype:实例变量类型

三要素:返回值类型,方法名,形参

形参:公共的可变(内容是可变的)的变量

例子1:不传入形参

创建一个Person类,其中包含两个成员变量name和age,并创建一个实例化方法来获得年龄, 并调用

Person.h文件

#import <Foundation/Foundation.h>@interface Person : NSObject{    @public    NSString *_name;    CGFloat _age;}- (CGFloat)getAge;@end
Person.m文件
#import "Person.h"@implementation Person- (CGFloat)getAge{    return _age;}@end
main 中
#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]) {    @autoreleasepool {               // 实例化方法用对象调用        Person *p = [[Person alloc] init];        p -> _age = 22;        CGFloat age = [p getAge];        NSLog(@"%.2f", age);    }    return 0;}

打印结果:

22.00


例子2:传入形参

升降排序

Order.h文件

<pre name="code" class="objc">#import <Foundation/Foundation.h>// 枚举// 改名     关键字   (存的类型,类型名)  typedef NS_ENUM(NSInteger, Aset){    Aset_ShengXu,    Aset_JiangXu};@interface Order : NSObject// 实例化方法- (void)sort:(int *)a count:(int)count aset:(Aset)aset;@end

Order.m文件
#import "Order.h"@implementation Order// aset为yes的时候是升序排列,为NO的时候是降序排列- (void)sort:(int *)a count:(int)count aset:(Aset)aset{    for (int i = 0; i < count - 1; i ++) {        for (int j = 0; j < count - i - 1; j ++) {                        if ([self judgeFirst:a[j] second:a[j + 1]] == aset) {                int temp = a[j];                a[j] = a[j + 1];                a[j + 1] = temp;            }        }    }        // 遍历数组    for (int i = 0; i < count; i++) {                NSLog(@"%d", a[i]);    }}- (Aset)judgeFirst:(int)a second:(int)b{    if (a > b) {// 第一个数 > 第二个数,交换位置                return Aset_ShengXu;// 按升序排列    }        return Aset_JiangXu;// 第一个数 < 第二个数,按降序排列}@end
main 中
#import <Foundation/Foundation.h>#import "Order.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        int a[5] = {2, 4, 1, 5, 3};        // 实例化方法:        // 创建对象再调用方法        Order *ord = [[Order alloc] init];        [ord sort:a count:5 aset:Aset_ShengXu];   }    return 0;}
打印结果:

1

2

3

4

5


4. 类方法( + 方法)

类调用方法

类方法什么时候用:

1) 当前方法和嵌套的方法中没有用到当前类的所有内容(成员变量,属性)

2) 简化代码

例子:将上面的实例化方法中的例子2用类方法写

Order.h文件

</pre><pre name="code" class="objc">#import <Foundation/Foundation.h>// 枚举// 改名     关键字   (存的类型,类型名)  typedef NS_ENUM(NSInteger, Aset){    Aset_ShengXu,    Aset_JiangXu};@interface Order : NSObject// 类方法+ (void)sort:(int *)a count:(int)count aset:(Aset)aset;@end

Order.m文件

#import "Order.h"@implementation Order// aset为yes的时候是升序排列,为NO的时候是降序排列+ (void)sort:(int *)a count:(int)count aset:(Aset)aset{    for (int i = 0; i < count - 1; i ++) {        for (int j = 0; j < count - i - 1; j ++) {                        if ([self judgeFirst:a[j] second:a[j + 1]] == aset) {                int temp = a[j];                a[j] = a[j + 1];                a[j + 1] = temp;            }        }    }        // 遍历数组    for (int i = 0; i < count; i++) {                NSLog(@"%d", a[i]);    }}- (Aset)judgeFirst:(int)a second:(int)b{    if (a > b) {// 第一个数 > 第二个数,交换位置                return Aset_ShengXu;// 按升序排列    }        return Aset_JiangXu;// 第一个数 < 第二个数,按降序排列}@end

main 中

#import <Foundation/Foundation.h>#import "Order.h"int main(int argc, const char * argv[]) {    @autoreleasepool {       int a[5] = {2, 4, 1, 5, 3};       // 类方法       // 类调用方法       [Order sort:a count:5 aset:Aset_JiangXu];    }    return 0;}
打印结果: 

5

4

3

2

1


5. 便利构造器

例子:

Person.h文件

#import <Foundation/Foundation.h>@interface Person : NSObject{    NSString *_name;    NSString *_sex;    NSInteger _number;}// 便利构造器 // 1. + 开头// 2. 返回值类型 id(对象)// 3. 类名personWith开头+ (id)personWithName:(NSString *)name number:(NSInteger)number sex:(NSString *)sex;+ (id)personWithName1:(NSString *)name1 sex1:(NSString *)sex1 age1:(NSInteger)age1;- (void)info;@end
Person.m文件
#import "Person.h"@implementation Person- (void)setName:(NSString *)name{    _name = name;}- (void)setSex:(NSString *)sex{    _sex = sex;}- (void)setAge:(NSInteger)age{    _age = age;}- (id)initWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age{    self = [super init];    if (self) {       _age = age;       _name = name;       _sex = sex;    }    return self;}// 便利构造器实现1 + (id)personWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age;{    Person *p = [[Person alloc] initWithName:name sex:sex age:age];    return p;}// 便利构造器实现2+ (id)personWithName1:(NSString *)name1 sex1:(NSString *)sex1 age1:(NSInteger)age1;{    Person *p = [[Person alloc] init];    p.name = name1;// 点语法 执行setter方法    p.age = age1;    p.sex = sex1;    return p;}- (void)info{    NSLog(@"name: %@, sex: %@, age: %lu", _name, _sex, _age);}@end
main 中
#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        Person *p =  [Person personWithName:@"Tom" sex:@"male" age:30];        [p info];                Person *p1 =  [Person personWithName1:@"Tom" sex1:@"male" age1:30];        [p1 info];            }    return 0;}
打印结果:

name: Tom, sex: male, age: 30

name: Tom, sex: male, age: 30











0 0
原创粉丝点击