iOS 中self和super如何理解?

原贴地址:http://www.cnblogs.com/Yukang1989/p/4116689.html


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

简单看下下面例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@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.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值