黑马程序员-OC语言基础学习(四)

来源:互联网 发布:魔法王座最新升阶数据 编辑:程序博客网 时间:2024/05/16 19:26

---------------------- IOS培训.期待与您交流! ----------------------

OC学习笔记四
---既然决定14年要好好的努力一次,那么就不要再轻言放弃,不再在随波逐流。只有努力过、拼搏过,才会知道自己行还是不行!为自己加油!---
---总结的知识点可能有不正确的地方,如果有发现请留言给我,我一定及时更正,不让错误的知识去误导大家,有好的意见也可大声提出来,小弟一定愿意采纳---

1、构造方法

Person *p = [Person new];
创建对象的new方法为类方法
通过new一个对象时,完整的创建一个可用的对象,进行两个操作
分配存储空间 +alloc
初始化 -init

new方法详解 1.调用+alloc分配内存空间,但是没有进行初始化
Person *p = [Person alloc];
2.调用-init方法进行初始化
Person *p1 = [p init];
将两步结合在一起 Person *p = [[Person alloc] init];

构造方法 :用来初始化对象的方法,构造方法一定是对象方法  init就是构造方法

2、构造方法--重写init方法
init初始化成员变量为0;对构造方法重写需要在@implementation中重写,不需要在@interface中定义

- (id) init
{
// 一定要调用super的inti方法;先初始化父类中的成员变量和属性
self = [super init];  //当前对象  self  这样保证self中有父类的变量和属性,同时也有自己定义类的属性和变量


//如果对象初始化成功,才有必要进行接下来的初始化
if(self != nil)
{
_age = 10;
}
//返回一个已经初始化完毕的对象
return self;
}

通过重写初始方法 -init 这样就把每一个创建的对象中的age变量初始值设置为10

重写构造方法练习

@interface Student : NSObject@property int no,age;@end@implementation : Student- (id) init{if( self = [super init]){_no = 1;_age = 10;}return self;}@end

重写构造方法的目的:为了让对象创建出来,成员变量就会有一些固定的值
重写构造方法注意点:
1)先调用父类的构造方法([super init])
2) 在进行子类内部成员的初始化

3、构造方法 - init方法的执行过程
从父类开始一级一级的向子类进行初始化

NSObject ---Person ----Student 

init 优先初始化NSObject 然后是Person 然后是Student

如 Student 继承NSObject 类 那么 super init 先初始化NSObject父类,然后在初始化 Student子类 

多级别的重写构造方法联系

@interface Person : NSObject@property int age;@end@implementation : Person- (id) init{if( self = [super init])    //在 Person中将 age成员变量 赋值为 10{_age = 10;}return self;}@end@interface Student : Person@property int no;@end@implementation : Student- (id) init{if( self = [super init])    //在 Person中将 no成员变量 赋值为 1{_no = 1;}return self;}@endint main(){Person *p = [[Person alloc] init];    //执行这个的时候会将age设置为10Student *s = [[Student alloc] init]//注意这个的执行顺序, 优先初始化 NSObject  然后 初始化 Person 并把age 赋初值 10 最后初始化 Student 把no 赋初值 1}



4、构造方法 --- 自定义构造方法

自定义构造方法需要遵循的一些规范:
1)一定是对象方法,一定要以 减号 - 开头
2)返回值一般是id类型
3)方法名一般以init开头  如 initWithName

练习一:@interface Person :NSObject@property int age, no;- (id) initWithAge:(int) age;@end@implementation :Person- (id) initWithAge:(int) age{if( self = [super init]){_age = age;}}@endint main(){Person *p = [[Person alloc] initWithAge:10];// 用传入的参数初始化成员变量}


练习二@interface Person :NSObject@property int age;    //Person 中有age - (int) initWithAge:(int) age;@end@implementation :Person- (int) initWithAge:(int) age{_age = age;}@end@interface Student :Person@property int no;//Student 中有no- (id) initWithAge:(int) age andNo:(int) no;   //初始化方法设置age 和no@end@implementation :Student- (id) initWithAge:(int) age andNo:(int) no{if( self = [super init]){self.age = age;    // 由于Student中没有age 所以需要去用父类中age   self 表示 父类初始化 所有用self 和点语法 进行设置age ;用super.age = age 也是可以的  [self setAge:age]_no = no;}/*这种方式要更优于上面的方式,思路更加的清晰,同时当父类中变量改变时,子类也不会收到影响。if( self = [super initWithAge:age]){_no = no;}*/}@endint main(){Person *p = [[Person alloc] initWithAge:10 andNo:3];// 用传入的参数初始化成员变量}

构造方法在创建对象时有着至关重要的作用,尤其在多层级的情况下,一定要区分好不同层级的初始化对象都包含那些变量和方法。

self = [super init]  可以理解为 父类对象,  就想没有父亲那会有儿子 ,所有 在构造方法重写和自定义时 self = [super init]  是非常重要的。



0 0
原创粉丝点击