zoukankan      html  css  js  c++  java
  • 一道题引发的self和super

    这个是那道题目,让写出输出的结果:
    刚看到这一道题目的时候我的第一反应就是输出Son     Father。但是输出的结果是Son Son。
    下面是解析:
         我首先建立了两个类,一个Father,一个Son
    Father.h:
    //
    //  Father.h
    //  SonFather
    //
    //  Created by zhanggui on 15/7/16.
    //  Copyright (c) 2015年 zhanggui. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Father : NSObject
    {
        NSString *name;
    }
    -(void)setName:(NSString *)youname;
    
    @end
     
    Father.m
    //
    //  Father.m
    //  SonFather
    //
    //  Created by zhanggui on 15/7/16.
    //  Copyright (c) 2015年 zhanggui. All rights reserved.
    //
    
    #import "Father.h"
    
    @implementation Father
    
    -(void)setName:(NSString *)youname
    {
        name = youname;
    }
    @end
       
    Son.h
     
    //
    //  Son.h
    //  SonFather
    //
    //  Created by zhanggui on 15/7/16.
    //  Copyright (c) 2015年 zhanggui. All rights reserved.
    //
    
    #import "Father.h"
    
    @interface Son : Father
    {
        NSUInteger age;
    }
    -(void)setAge:(NSUInteger)youage;
    -(void)setName:(NSString *)youname andAge:(NSUInteger)youage;
    
    @end
     
    Son.m
    //
    //  Son.m
    //  SonFather
    //
    //  Created by zhanggui on 15/7/16.
    //  Copyright (c) 2015年 zhanggui. All rights reserved.
    //
    
    #import "Son.h"
    
    @implementation Son
    
    -(id)init
    {
        self = [super init];
        if (self) {
            NSLog(@"%@",NSStringFromClass([self class]));
            NSLog(@"%@",NSStringFromClass([super class]));
        }
        return self;
    }
    -(void)setName:(NSString *)youname andAge:(NSUInteger)youage
    {
        [self setAge:youage];
        [super setName:youname];
    }
    -(void)setAge:(NSUInteger)youage
    {
        age = youage;
    }
    @end
    

      

     
    然后我加入下面两个输出:
    -(void)setName:(NSString *)youname andAge:(NSUInteger)youage
    {
        [self setAge:youage];
        [super setName:youname];
        NSLog(@"class is %@",[self class]);
        NSLog(@"super class is %@",[super class]);
    }
    

      

    输出的结果是:
    class is Son
    super class is Son
    运行中init那里面输出的也是
    Son
    Son
         其实,self是类的隐藏的参数(另一个隐藏参数是_cmd,代表当前类方法的selector),指向当前调用方法的类.super并不是一个隐藏的参数,它只是一个编译器指示符,它和self指向的是相同的消息接收者。
         就像[self class]    [super class]。接收class这个方法的接收者都是Son这个对象。不同的是,super告诉编译器,当使用super时,要去调用父类的方法,而不是本类里的方法。
          当使用self调用方法时,会从当前类中方法列表中查找,如果没有,就从父类方法中查找;而当使用super时,则直接从父类方法中查找,然后调用父类的方法。
       
    更深一步
         这种机制是当调用类方法的时候,编译器会将方法调用转成一个C函数方法调用.Apple的objcRuntimeRef上说:
     
          Sending Messages
     
      When it encounters a method invocation, the compiler might generate a call to any of several functions to perform the actual message dispatch, depending on the receiver, the return value, and the arguments. You can use these functions to dynamically invoke methods from your own plain C code, or to use argument forms not permitted by NSObject’s perform… methods. These functions are declared in /usr/include/objc/objc-runtime.h.
      objc_msgSend sends a message with a simple return value to an instance of a class.
      objc_msgSend_stret sends a message with a data-structure return value to an instance of a class.
           objc_msgSendSuper sends a message with a simple return value to the superclass of an instance of a class.
      objc_msgSendSuper_stret sends a message with a data-structure return value to the superclass of an instance of a class.
    因此,当使用[self init],会调用objc_msgSend。我们看一下objc_msgSend定义:
                   id objc_msgSend(id theReceiver, SEL theSelector, ...)
                  第一个参数是消息接收者,第二个参数是调用具体类方法的selector,后面是selector方法的可变参数。当执行[self class]时,编译器会替换成调用objc_msgSend的函数调用,其中theReceive是self,therSelector是@selector(class),这个selector是从当前self的类方法中查找的,找到后吧对应selector传递过去。
              而当执行[super class],会调用objc_msgSendSuper函数,下边看一下objc_msgSendSuper函数定义:
               struct objc_super { id receiver; Class superClass; };
         可以看到该结构体包含两个成员,一个是receiver,第二个是记录写下super这个类的父类是什么。举个例子,当调用哪[super setName:]时,开始做以下几件事
         构建objc_super的结构体,此时这个结构体第一个成员变量receiver就是Son *son1,和self相同。而第二个成员变量superclass是指Person,因为Son的超类就是Person.
         调用objc_msgSendSuper方法,将这个结构体和setName的sel传递过去。从 objc_super 结构体指向的 superClass 的方法列表开始找 setName 的 selector,找到后再以 objc_super->receiver 去调用这个 selector,可能也会使用 objc_msgSend 这个函数,不过此时的第一个参数 theReceiver 就是 objc_super->receiver,第二个参数是从 objc_super->superClass 中找到的 selector。
     
     
    回过来看代码:
     
    当使用 [self class] 时,这时的 self 是 Son,在使用 objc_msgSend 时,第一个参数是 receiver 也就是 self,也是 Son* son1 这个实例。第二个参数,要先找到 class 这个方法的 selector,先从 PersonMe 这个类开始找,没有,然后到 PersonMe 的父类 Person 中去找,也没有,再去 Person 的父类 NSObject 去找,一层一层向上找之后,在 NSObject 的类中发现这个 class 方法,而 NSObject 的这个 class 方法,就是返回 receiver 的类别,所以这里输出 PersonMe。
     
      当使用 [super class] 时,这时要转换成 objc_msgSendSuper 的方法。先构造 objc_super 的结构体吧,第一个成员变量就是 self,第二个成员变量是 Father,然后要找 class 这个 selector,先去 superClass 也就是 Father 中去找,没有,然后去 Person 的父类中去找,结果还是在 NSObject 中找到了。然后内部使用函数 objc_msgSend(objc_super->receiver, @selector(class)) 去调用,此时已经和我们用 [self class] 调用时相同了,此时的 receiver 还是Son *son1,所以这里返回的也是 Son。
     
     
    参考来源:
  • 相关阅读:
    CentOS 编译安装 MySQL5.7
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了
    Linux里如何查找文件内容
    linux怎么模糊查找一个文件
    centos7下使用yum安装mysql
    centos下完全卸载mysql
    Linux下安装配置Nexus
    Linux下建立Nexus私服
    阿里云主机上安装jdk
    java war run
  • 原文地址:https://www.cnblogs.com/zhanggui/p/4651010.html
Copyright © 2011-2022 走看看