或许你理解self和super都是指的是类的对象   self指的是本类的对象,而super指的是父类的对象,但是事实情况呢,可能有些和你想象的不一样?

简单看下下面例子:

@interface Person:NSObject {
NSString* name;
}
- (void) setName:(NSString*) yourName;
@end @interface PersonMe:Person {
NSUInteger age;
} - (void) setAge:(NSUInteger) age;
- (void) setName:(NSString*) yourName andAge:(NSUInteger) age;
@end @implementation PersonMe
- (void) setName:(NSString*) yourName andAge:(NSUInteger) age {
[self setAge:age]; [super setName:yourName];
  NSLog(@"self ' class is %@", [self class]);
NSLog(@"super' class is %@", [super class]);
}
@end int main(int argc, char* argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; PersonMe* me = [[PersonMe alloc] init];
[me setName:@"asdf" andAge:18];
[me release]; [pool drain];
return 0;
}

  

按照之前的理解,很简单得出结论:

self ' s class is PersonMe super ' s class is Person

但是实际运行的结果是:

self 's class is PersonMe super ' s class is PersonMe

self 的 class 和预想的一样,怎么 super 的 class 也是 PersonMe?

真正的原因在哪里呢?

self是一个隐私参数,熟悉C的都知道,他和 _cmd构成方法的参数,self是动态的;执行时调用RT的objc_msgSend();

super是个编译器的指令符号,只是告诉编译器在执行的时候,去调谁的方法.super是编译的;执行时调用RT的objc_msgSendSuper();

英文解释如下:

self refers to the object receiving a message in objective-C programming.

Invoking a method on the self searches for the method implementation of the method in the usual manner, starting in the dispatch table of the receiving object’s class.

Example: [self startThread]; self.hostReach = YES; BOOL value = self.hostReach;

1.self is also a variable name that can be used in any number of ways, even assigned a new value. 2.Inside an instance method, self refers to the instance; but inside a class method, self refers to the class object.

super is a flag that tells the compiler to search for the method implementation in a very different place. It begins in the superclass of the class that defines the method where super appears.

Wherever super receives a message, the compiler substitutes another messaging routine for the objc_msgSend function. The substitute routine looks directly to the superclass of the defining class—that is, to the superclass of the class sending the message to super—rather than to the class of the object receiving the message.Messages to super allow method implementations to be distributed over more than one class.

For some tasks, each class in the inheritance hierarchy can implement a method that does part of the job and passes the message on to super for the rest. The init method, which initializes a newly allocated instance, is designed to work like this. Each init method has responsibility for initializing the instance variables defined in its class. But before doing so, it sends an init message to super to have the classes it inherits from initialize their instance variables. Each version of init follows this procedure, so classes initialize their instance variables in the order of inheritance.

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocDefiningClasses.html

iOS 中self和super如何理解?的更多相关文章

  1. Java泛型中extends和super的理解(转)

    E – Element (在集合中使用,因为集合中存放的是元素) T – Type(Java 类) K – Key(键) V – Value(值) N – Number(数值类型) ? – 表示不确定 ...

  2. iOS 中架构模式的浅显理解

    我们开发软件中应用各种模式,主要是为了 职责划分:一个类只做一件事 易用,可维护,方便扩展 解耦,相互独立,可单独测试 各种设计模式其实都是在解决上面的问题,让我们对比看看吧. 一.如何理解MVC设计 ...

  3. Java泛型中extends和super的理解

    作者:zhang siege链接:https://www.zhihu.com/question/20400700/answer/91106397来源:知乎著作权归作者所有.商业转载请联系作者获得授权, ...

  4. iOS中autolaylout和sizeclass的理解

    没发现居然有三四个月没写博客了,好惭愧.都是加班太多了,还好现在换了一家,还是得继续写啊. 主要是学习了http://onevcat.com/上的内容写的笔记,并自己动手操作了一下. 已经排好版了,懒 ...

  5. Java 泛型 <? super T> 中 super 怎么 理解?与 < ? extends T>有何不同?

    Java 泛型 <? super T> 中 super 怎么 理解?与 extends 有何不同? 简介 前两篇文章介绍了泛型的基本用法.类型擦除以及泛型数组.在泛型的使用中,还有个重要的 ...

  6. iOS中的堆(heap)和栈(stack)的理解

    操作系统iOS 中应用程序使用的计算机内存不是统一分配空间,运行代码使用的空间在三个不同的内存区域,分成三个段:“text segment “,“stack segment ”,“heap segme ...

  7. iOS中的存储方式

    1.Plist 1.1 了解沙盒 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其它文件系统隔离.应用必须呆在自己的沙盒里.其它应用不能访问该沙盒. 一个程序中所有的非代码文件都可以 ...

  8. IOS中的多线程之GCD

    在ios中,使用多线程有三种方式,分别是:NSThread.NSOperation和NSOperationQueue.GCD,在本节,主要讲解一下CDD的使用. GCD(Grand Central D ...

  9. 浅谈iOS中的单例模式

    iOS中的单例模式     就我本身理解而言,我认为的单例:单例在整个工程中,就相当于一个全局变量,就是不论在哪里需要用到这个类的实例变量,都可以通过单例方法来取得,而且一旦你创建了一个单例类,不论你 ...

随机推荐

  1. 如何在Android studio中同时打开多个工程? (转载)

    最近学习Android Studio,想同时打开两个Project.但是点击File->Open之后,原有的Project被关闭掉了.怎么在新的窗口中打开Project呢? 解决: 点击Help ...

  2. 漫游Kafka设计篇之性能优化

    Kafka在提高效率方面做了很大努力.Kafka的一个主要使用场景是处理网站活动日志,吞吐量是非常大的,每个页面都会产生好多次写操作.读方面,假设每个消息只被消费一次,读的量的也是很大的,Kafka也 ...

  3. Git之 基本常用命令

    Git基本常用命令如下: mkdir:         XX (创建一个空目录 XX指目录名) pwd:          显示当前目录的路径. git init          把当前的目录变成可 ...

  4. BZOJ 4690 Never Wait for Weights

    带权并查集23333333 注意dis[x]+=dis[fath[x]. #include<iostream> #include<cstdio> #include<cst ...

  5. 使用sqlldr将文件中的数据导入到数据库

    1.创建数据文件: ?如,在D:\创建 zhaozhenlong.txt 文件,文件内容为: 11,12,1321,22,2331,32,33 2.创建控制文件: 如,在D:\创建 zhaozhenl ...

  6. 【转】Github轻松上手4-常用的git命令

    转自:http://blog.sina.com.cn/s/blog_4b55f6860100zzih.html 附上一些git的常见命令: •    git remote add origin git ...

  7. vs2008破解方法

    前提条件:测试操作系统WIN7 1,解压缩镜像文件 2,找到文件:Setup\setup.sdb,用记事本打开: 3,找到以下项: [Product Key]XPWKC7X98VKQDGM3QWYVG ...

  8. JBPM4入门——5.流程定义的发布、查询、删除

    本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...

  9. 【转】Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得

    iOS模拟器简介 iOS功能简介 iOS模拟器,是在Mac下面开发程序时,开发iOS平台的程序时候,可以使用的辅助工具. 其功能是,帮你模拟iOS平台设备,在模拟器上运行对应的程序,以方便你没有实体设 ...

  10. 自定义View(三)--实现一个简单地流式布局

    Android中的流式布局也就是常说的瀑布流很是常见,不仅在很多项目中都能见到,而且面试中也有很多面试官问道,那么什么是流式布局呢?简单来说就是如果当前行的剩余宽度不足以摆放下一个控件的时候,则自动将 ...