您的位置:首页 > 移动开发 > Objective-C

Objective-C中的self关键词

2015-05-29 17:23 417 查看
self在OC中指当前类或者当前对象。
判断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是类名,不同类有不同标识。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: