iOS self和super的区别

self和super的区别

#import <Foundation/Foundation.h>

首先先写两个类 fist和two,two继承fist类

@interface First:NSObject

{

    int a;//声明了一个变量

}

-(void) setA:(int)c;//对a赋值的方法

@end

@implementation First

 

-(void)setA:(int)c

{

    a=c;

}

 

@end

 

//类2

@interface Two : First

{

    int b;

}

-(void)setB:(int)d;

-(void)print;

@end

@implementation Two

-(void)setB:(int)d

{

    b=d;

}

-(void)print

{

    NSLog(@"a的值为%d b的值为%d",a,b);

    NSLog(@"这时self==%@",[self class]);

    NSLog(@"super=====%@",[superclass]);

}

 

@end

 

 

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

            

        Two *two=[[Two alloc] init];

        NSLog(@"这时frist中的函数");

        [two setB:2];

        NSLog(@"这是two中的函数");

        [two setA:1];

        

        [two print];

        

      

    }

    return 0;

}

运行的结果是:

这时我们看到self和super的值一样  区别在哪呢????

其实是这样的  self和super都是指的是类的对象   self指的是本类的对象,而super指的是父类的对象

他们相当于系统自己声明的一个类对象  ,,,,,

原文地址:https://www.cnblogs.com/flyingdreaming/p/selfandsuper.html