Objective-C中的self关键词

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 *)newName 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是类名,不同类有不同标识。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值