Objective-C中的self关键词
2015-05-29 17:23
417 查看
self在OC中指当前类或者当前对象。
判断self指的时当前类还是当前对象的依据就是方法的类型。
+方法(静态方法)中的self指的是当前类。
-方法中的self指的时当前对象。
首先给出一个完整代码,通过这个代码来解释self的用法。
声明文件如下:
main函数中创建对象和调用对象成员函数。
上面函数就是演示如何调用函数,并没有打印输出,所以执行程序后没有显示结果。
下面对其中部分代码做解释。
虽然也可以使用 _age = newAge;这样的语句,但是更推荐通过调用已经实现的函数来实现赋值。
对象初始化的时候还有一个参数和没有参数两种情况,分别可以通过调用上面已经实现了的三个参数的构造函数就可以实现对应功能。
这里的self指的是当前对象。
[self alloc]也就相当于[Person alloc] 。 但是因为可移植性等原因更推荐使用self关键字。
-------------------------
程序运行的过程是:
我们编写好的OC语言首先会被解释转换成C语言,然后转换成二进制编码,最后在iPhone上面运行。
假设有下面的一个方法。
事实上在转换成C语言的时候就会转换为类似下方的代码:
其中,person是类名,不同类有不同标识。
判断self指的时当前类还是当前对象的依据就是方法的类型。
+方法(静态方法)中的self指的是当前类。
-方法中的self指的时当前对象。
首先给出一个完整代码,通过这个代码来解释self的用法。
声明文件如下:
@interface Person : NSObject { @private // 定义一个Person类,有两个成员变量 int _age; NSString *_name; } // 构造函数各种变种 - (id) init; - (id) initWithName:(NSString *)newName; - (id) initWithName:(NSString *)newName withAge:(int)newAge; - (void) setAge:(int)newAge; - (int) getAge; - (void) setName:(NSString *)newName; - (NSString *) getName; - (void) setName:(NSString *)newName withAge:(int)newAge; + (id) person; + (id) personWithName:(NSString *)newName withAge:(int)newAge; @end定义文件如下:
@implementation Person - (id) init { return [self initWithName:@"无名氏"]; } - (id) initWithName:(NSString *)newName { return [self initWithName:newName withAge:-1]; } - (id) initWithName:(NSString *)newName withAge:(int)newAge { // super表示父对象,基类的对象。 self = [super init]; if (self) { [self setName:newName withAge:newAge]; NSLog(@"在构造函数中 name %@ age %d %s", _name, _age, __FUNCTION__); } return self; } - (void) setAge:(int)newAge{ _age = newAge; } - (int) getAge{ return _age; } - (void) setName:(NSString *)newName { _name = newName; } - (NSString *) getName { return _name; } - (void) setName:(NSString *)newName withAge:(int)newAge { [self setName:newName]; [self setAge:newAge]; } + (id) person { return [[self alloc] init]; } + (id) personWithName:(NSString *)newName withAge:(int)newAge { id obj = [[self alloc] initWithName:newName withAge:newAge]; return obj; }
@end<span style="white-space:pre"> </span>
main函数中创建对象和调用对象成员函数。
int main(int argc, const char * argv[]) { @autoreleasepool { Person *xiaoming = [[Person alloc] init]; [xiaoming setAge:20]; [xiaoming setName:@"小明"]; Person *xiaoli = [[Person alloc] init]; [xiaoli setName:@"小李" withAge:30]; Person *xiaozhang = [[Person alloc] initWithName:@"小张" withAge:50]; Person *xiaoliu = [Person person]; [xiaoliu setName:@"xialiu" withAge:33]; } return 0; }
上面函数就是演示如何调用函数,并没有打印输出,所以执行程序后没有显示结果。
下面对其中部分代码做解释。
- (void) setName:(NSString *)newName withAge:(int)newAge { [self setName:newName]; [self setAge:newAge]; }这里的self指当前对象,通过调用当前对象的setName和setAge函数来设置名字和年龄。
虽然也可以使用 _age = newAge;这样的语句,但是更推荐通过调用已经实现的函数来实现赋值。
- (id) initWithName:(NSString *)newName withAge:(int)newAge { // super表示父对象,基类的对象。 self = [super init]; if (self) { [self setName:newName withAge:newAge]; NSLog(@"在构造函数中 name %@ age %d %s", _name, _age, __FUNCTION__); } return self; }这里是实现构造函数,对有两个参数的对象进行初始化。
对象初始化的时候还有一个参数和没有参数两种情况,分别可以通过调用上面已经实现了的三个参数的构造函数就可以实现对应功能。
这里的self指的是当前对象。
+ (id) personWithName:(NSString *)ne 4000 wName withAge:(int)newAge { id obj = [[self alloc] initWithName:newName withAge:newAge]; return obj; }这里的self指的是当前类,即Person类。
[self alloc]也就相当于[Person alloc] 。 但是因为可移植性等原因更推荐使用self关键字。
-------------------------
程序运行的过程是:
我们编写好的OC语言首先会被解释转换成C语言,然后转换成二进制编码,最后在iPhone上面运行。
假设有下面的一个方法。
- (void) setName:(NSString *)newName withAge:(int)newAge { [self setName:newName]; [self setAge:newAge]; }
事实上在转换成C语言的时候就会转换为类似下方的代码:
int __person_XXXX_setNameWithAge(id self, int newName, int newAge) { self->_age =newAge; self->_name = newName; }
其中,person是类名,不同类有不同标识。
相关文章推荐
- Objective-C对象的申请空间与初始化
- WebService传输SoapObject数据出现 org.ksoap2.serialization.SoapObject cannot be cast to java.util.Vector的问题
- Java核心API -- 1(Object类)
- 浏览器提示Uncaught TypeError: object is not a function XXX onclick
- IOS开发系列—Objective-C之Foundation框架
- JQuery创建object 并自定义键、值(获取table中数据,封装成一个对象)
- UITableViewCell Property “icon” cannot be found in forward class object “DJWeiBo”
- 使用objection来模块化开发iOS项目
- 条款21:必须返回对象object时,不要返回其引用reference
- Cocoa 框架 For iOS(一) 框架的介绍,Objectivie-C运行时能力的解析等
- BING : Binarized Normed Gradients for Objectness Estimation at 300fps 论文笔记
- Windows之SignalObjectAndWait
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Chapter12. Foundation Classes and selector in Objective-C
- oc中谓词的使用(NSPredicate)
- 调用nil对象的方法(Calling Methods on Nil)
- oc中数组的四种遍历方式及比较
- IOS开发学习27 ObjectC 自带json解析方法的使用
- Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved tran
- 泛型限定<? extends Object>