iOS開發實用技巧—super、superClass、class的區別


class:獲取方法調用類名
superclass:獲取方法調用者的父類類名
super:編譯修飾符,不是指針,指向父類標志,
本質還是拿到當前對象去調用父類的方法
注意:super並不是拿到父類對象去調用父類方法
 
 1 #import <Foundation/Foundation.h>
 2 @interface Person : NSObject
 3 - (void)test;
 4 @end
 5  
 6 #import "Person.h"
 7 @implementation Person
 8 - (void)test
 9 {
10     // self:SonPerson
11     // 輸出結果 SonPerson Person self:SonPerson
12      NSLog(@"%@ %@ %@",[self class],[self superclass],[super class]);
13 }
14 @end
15  
16 #import "Person.h"
17 
18 @interface SonPerson : Person
19 @end
20  
21 #import "SonPerson.h"
22 
23 @implementation SonPerson
24 - (void)test
25 {
26     // 輸出結果:SonPerson Person SonPerson
27 //    NSLog(@"%@ %@ %@",[self class],[self superclass],[super class]);
28     [super test];
29 }
30  
31 #import "ViewController.h"
32 
33 #import "SonPerson.h"
34 
35 @interface ViewController ()
36 
37 @end
38  
39  
40 #import <UIKit/UIKit.h>
41 @interface ViewController : UIViewController
42 @end
43  
44 @implementation ViewController
45 - (void)viewDidLoad {
46     [super viewDidLoad];
47     SonPerson *son = [[SonPerson alloc] init];
48     [son test];
49 }
50 @end
輸出結果:SonPerson Person SonPerson


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM