Inheritance of Objective-C classes, use of self and super pointers, method rewriting

General Catalog iOS Development Notes Catalog From Nothing to Getting Started

Article Directory

Intro

The following content includes:

  • The subclass inherits the class declaration of the parent class;
  • The subclass inherits the method call characteristics after the parent class;
  • The use of self and super;
  • self represents the current object in the object method, and represents the class in the class method (you can use %p to print the pointer value proof inside the method);
  • How to rewrite the method inherited from the parent class in the subclass, and use self and super to call the method in the subclass method after rewriting;

Demo

the code

//
//  main.m
//  类的继承
//
//  Created by wuyujin1997 on 2023/2/25.
//

#import <Foundation/Foundation.h>

@interface Father : NSObject {
    
    
    NSString* _fatherField;
}
+ (void) fatherClassFunc;
- (void) fatherObjFunc;
// getter & setter
- (NSString*) fatherField;
- (void) setFatherField:(NSString*)fatherField;
@end
@implementation Father
+ (void) fatherClassFunc {
    
    
    NSLog(@"父类的类方法");
}
- (void) fatherObjFunc {
    
    
    NSLog(@"父类的成员方法");
}
- (NSString*) fatherField {
    
    
    return self->_fatherField;
}
- (void) setFatherField:(NSString*)fatherField {
    
    
    self->_fatherField = fatherField;
}
@end

@interface Son : Father {
    
    
    NSString* _sonField;
}
+ (void) sonClassFunc;
- (void) sonObjFunc;
// getter & setter
- (NSString*) sonField;
- (void) setSonField:(NSString*)sonField;
@end

@implementation Son

+ (void) sonClassFunc {
    
    
    NSLog(@"===>子类的类方法 start 子类的类方法中可以使用 super 调用继承自父类的类方法");
    [super fatherClassFunc];    // 这四种写法都会可以调用到继承自父类、现在也属于子类的方法。
    [self fatherClassFunc];
    [Father fatherClassFunc];
    [Son fatherClassFunc];
    NSLog(@">>>>>>>>>>>类方法中的self地址: %p", self);
    NSLog(@"===>子类的类方法   end");
}

- (void) sonObjFunc {
    
    
    NSLog(@"===>子类的对象方法 start 子类对象方法中可以调用继承自父类的对象方法");
    [super fatherObjFunc];  // 一定会调用父类的该方法
    [self fatherObjFunc];   // 如果子类重写了该方法,就调用子类自己的。如果没有,这个self就相当于super,会去调用父类的。
    NSLog(@"子类方法中访问子类的成员属性:%@", self->_sonField);
//    NSString* fatherField = super->_fatherField; // Use of undeclared identifier 'super'
    NSLog(@">>>>>>>>>>>对象方法中的self地址:%p", self);
    NSLog(@"===>子类的对象方法   end");
}

- (NSString*) sonField {
    
    
    return self->_sonField;
}
- (void) setSonField:(NSString*)sonField {
    
    
    self->_sonField = sonField;
}

// 子类中重写一个与父类方法同名的方法
- (void) fatherObjFunc {
    
    
    NSLog(@"子类中重写的与一个父类方法同名的方法");
}

@end

int main(int argc, const char * argv[]) {
    
    
    NSLog(@"类的继承中属性和方法的继承特性、子类重写继承自父类的方法self和super指针的使用");
    
    Father* fatherObj = [Father new];
    [fatherObj fatherObjFunc];
    [Father fatherClassFunc];
    // 对象不能去调用类方法
//    [p fatherClassFunc]; // No visible @interface for 'Father' declares the selector 'fatherClassFunc'
    
    Son* sonObj = [Son new];
    [sonObj setFatherField:@"fatherFieldValue"];    // 子类对象调用继承自父类的成员方法。
    [sonObj setSonField:@"sonFieldValue"];
    [sonObj sonObjFunc];
    [Son sonClassFunc];
    NSLog(@">>>>>>>>>>>Son类的地址:%p", [Son class]);
    NSLog(@">>>>>>>>>>>son对象的地址:%p", sonObj);
    
    return 0;
}

Output

Note that the two values ​​of the printed Son object address are the same: 0x600000202140, and the two values ​​of the class address (Son's Class object) are also the same: 0x100008258.

2023-02-27 22:35:50.330564+0800 类的继承[39283:1161074] 类的继承中属性和方法的继承特性、子类重写继承自父类的方法self和super指针的使用
2023-02-27 22:35:50.330816+0800 类的继承[39283:1161074] 父类的成员方法
2023-02-27 22:35:50.330838+0800 类的继承[39283:1161074] 父类的类方法
2023-02-27 22:35:50.330857+0800 类的继承[39283:1161074] ===>子类的对象方法 start 子类对象方法中可以调用继承自父类的对象方法
2023-02-27 22:35:50.330872+0800 类的继承[39283:1161074] 父类的成员方法
2023-02-27 22:35:50.330887+0800 类的继承[39283:1161074] 子类中重写的与一个父类方法同名的方法
2023-02-27 22:35:50.330907+0800 类的继承[39283:1161074] 子类方法中访问子类的成员属性:sonFieldValue
2023-02-27 22:35:50.330933+0800 类的继承[39283:1161074] >>>>>>>>>>>对象方法中的self地址:0x600000202140
2023-02-27 22:35:50.331082+0800 类的继承[39283:1161074] ===>子类的对象方法   end
2023-02-27 22:35:50.331183+0800 类的继承[39283:1161074] ===>子类的类方法 start 子类的类方法中可以使用 super 调用继承自父类的类方法
2023-02-27 22:35:50.331235+0800 类的继承[39283:1161074] 父类的类方法
2023-02-27 22:35:50.331297+0800 类的继承[39283:1161074] 父类的类方法
2023-02-27 22:35:50.331356+0800 类的继承[39283:1161074] 父类的类方法
2023-02-27 22:35:50.331424+0800 类的继承[39283:1161074] 父类的类方法
2023-02-27 22:35:50.331487+0800 类的继承[39283:1161074] >>>>>>>>>>>类方法中的self地址: 0x100008258
2023-02-27 22:35:50.331546+0800 类的继承[39283:1161074] ===>子类的类方法   end
2023-02-27 22:35:50.331605+0800 类的继承[39283:1161074] >>>>>>>>>>>Son类的地址:0x100008258
2023-02-27 22:35:50.331664+0800 类的继承[39283:1161074] >>>>>>>>>>>son对象的地址:0x600000202140
Program ended with exit code: 0

Guess you like

Origin blog.csdn.net/wuyujin1997/article/details/129251529