黑马程序员-OC-自定义构造方法和description方法
2015-03-29 15:44
501 查看
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
使用init方法初始化的对象其初始值是0,而有些时候我们想让其初始值是其他值,这样我们就要重写init方法来实现我们的目的。
自定义构造方法:
构造方法:用来初始化对象的方法,是个对象方法,-开头
重写构造方法的目的:为了让对象创建出来,成员变量就会有一些固定的值
1.在Person.h中的声明:
自定义构造方法的规范
1.一定是对象方法,一定以 - 开头
2.返回值一般是id类型
3.方法名一般以initWith开头
2.在Person.m中构造方法的实现:
重写init方法:
* 跟Java一样,构造方法内部首先要调用父类的构造方法,再调用了父类的init方法,它会返回初始化好的Student对象,这里把返回值赋值给了self,self代表Person对象本身
* 第3~5行的意思是:如果self不为nil,也就是初始化成功,就给成员变量_age进行赋值
* 最后返回初始化过后的self,整个构造方法就结束了
以上方法可简化为:
构造方法的调用:
重写构造方法的注意点
1.先调用父类的构造方法([super init])2.再进行子类内部成员变量的初始化 完整地创建一个可用的对象 1.分配存储空间 +alloc 2.初始化 -init
description方法
-description(对象方法)
使用NSLog和@%输出某个对象时,会调用对象的description方法,并拿到返回值进行输出。
+description(类方法)
使用NSLog和@%输出某个类对象时,会调用类对象的description方法,并拿到返回值进行输出。
输出结果为:
以上这样的情况下:
1.会调用对象p的-description方法
2.拿到-description方法的返回值(NSString *)显示到屏幕上
3.-description方法默认返回的是“类名+内存地址”
默认情况下,利用NSLog和%@输出对象时,结果结构是:<类名:内存地址>
要注意%@只能用于OC对象,不能用于其它语言
以下这种情况调用的是+description方法:
重写description方法:
由于description输出的是<类名:内存地址>,但在通常情况下我们是不在乎类名和内存地址的,所以通常我们都会重写description方法来覆盖默认方法。
代码示例:
注意:
一下写法是错误的,不要轻易编写:
使用init方法初始化的对象其初始值是0,而有些时候我们想让其初始值是其他值,这样我们就要重写init方法来实现我们的目的。
自定义构造方法:
构造方法:用来初始化对象的方法,是个对象方法,-开头
重写构造方法的目的:为了让对象创建出来,成员变量就会有一些固定的值
1.在Person.h中的声明:
- (id)initWithName:(NSString *)name; - (id)initWithAge:(int)age; - (id)initWithName:(NSString *)name andAge:(int)age;
自定义构造方法的规范
1.一定是对象方法,一定以 - 开头
2.返回值一般是id类型
3.方法名一般以initWith开头
2.在Person.m中构造方法的实现:
重写init方法:
- (id)init { // 1.一定要调用回super的init方法:初始化父类中声明的一些成员变量和其他属性 self = [super init]; // 当前对象 self // 2.如果对象初始化成功,才有必要进行接下来的初始化 if (self != nil) { // 初始化成功 _age = 10; } // 3.返回一个已经初始化完毕的对象 return self; }
* 跟Java一样,构造方法内部首先要调用父类的构造方法,再调用了父类的init方法,它会返回初始化好的Student对象,这里把返回值赋值给了self,self代表Person对象本身
* 第3~5行的意思是:如果self不为nil,也就是初始化成功,就给成员变量_age进行赋值
* 最后返回初始化过后的self,整个构造方法就结束了
以上方法可简化为:
- (id)initWithName:(NSString *)name { if ( self = [super init] ) { _name = name; } return self; } - (id)initWithAge:(int)age { if ( self = [super init] ) { _age = age; } return self; } - (id)initWithName:(NSString *)name andAge:(int)age { if ( self = [super init] ) { _name = name; _age = age; }
构造方法的调用:
<pre name="code" class="html" style="line-height: 26px;"> Student *p = [[Student alloc] initWithName:@"Jim"];
<pre name="code" class="html" style="line-height: 26px;"> Student *p = [[Student alloc] initWithAge:29];
<pre name="code" class="html" style="line-height: 26px;"> Student *p = [[Student alloc] initWithName:@"Jim" andAge:29 andNo:10];
重写构造方法的注意点
1.先调用父类的构造方法([super init])2.再进行子类内部成员变量的初始化 完整地创建一个可用的对象 1.分配存储空间 +alloc 2.初始化 -init
description方法
-description(对象方法)
使用NSLog和@%输出某个对象时,会调用对象的description方法,并拿到返回值进行输出。
+description(类方法)
使用NSLog和@%输出某个类对象时,会调用类对象的description方法,并拿到返回值进行输出。
<span style="font-size:14px;">1 Student *p = [[Student alloc] initWithAge:10]; 2 3 NSLog(@"%@", p); 4 5 [p release];</span>
输出结果为:
<Student: 0x100109910>
以上这样的情况下:
1.会调用对象p的-description方法
2.拿到-description方法的返回值(NSString *)显示到屏幕上
3.-description方法默认返回的是“类名+内存地址”
默认情况下,利用NSLog和%@输出对象时,结果结构是:<类名:内存地址>
要注意%@只能用于OC对象,不能用于其它语言
以下这种情况调用的是+description方法:
Class c = [Person class]; NSLog(@"%@", c);
重写description方法:
由于description输出的是<类名:内存地址>,但在通常情况下我们是不在乎类名和内存地址的,所以通常我们都会重写description方法来覆盖默认方法。
代码示例:
- (NSString *)description { return [NSString stringWithFormat:@"age=%d, name=%@", _age, _name]; } // 决定了类对象的输出结果 + (NSString *)description { return @"Abc"; }
注意:
一下写法是错误的,不要轻易编写:
- (NSString *)description { // 使用下面代码会引发死循环 NSLog(@"%@", self); }以上代码同时使用了%@和self,代表要调用self的description方法,因此最终会导致程序陷入死循环,循环调用description方法。
相关文章推荐
- 黑马程序员_OC基础04_description方法(自定义打印输出方法)
- 黑马程序员-OC的构造方法init以及自定义构造方法
- 黑马程序员——OC 基础:实例变量修饰符,description方法和多态
- OC_构造方法,自定义构造方法,分类,类的深入研究,description,SEL数据类型笔记
- 黑马程序员——OC语言基础---description方法介绍及重写
- 黑马程序员——OC语言学习——关键字id、构造方法、Category-分类、类的本质、 description方法、 SEL方法
- 【Objective-C】07-自定义构造方法和description方法
- 学习笔记之07-自定义构造方法和description方法
- 黑马程序员-OC-description方法
- 【Objective-C】07-自定义构造方法和description方法
- 黑马程序员-------OC----构造方法和description方法
- Chapter11. Objective-C learning note 4. 自定义构造方法和description方法
- 【Objective-C】07-自定义构造方法和description方法
- 【Objective-C】07-自定义构造方法和description方法
- 【Objective-C】07-自定义构造方法和description方法
- 黑马程序员_OC基础08_description方法及SEL
- 05-自定义构造方法和description方法(%@带打印一个对象的时候,会调用这个方法)
- 黑马程序员——Objective-C——构造方法和description方法
- 3-自定义构造方法和description方法
- Object-C基础-04-自定义构造方法和description方法