Objective-C 类的继承、self和super指针的使用、方法的重写

总目录 iOS开发笔记目录 从一无所知到入门

文章目录

Intro

以下内容包含:

  • 子类继承父类的类声明;
  • 子类继承父类后的方法调用特性;
  • self和super的使用;
  • self在对象方法中表示当前对象,在类方法中表示本类(可以在方法内部使用 %p打印该指针值证明);
  • 子类中如何重写继承自父类的方法、重写后在子类方法中使用self和super调用该方法的效果;

Demo

代码

//
//  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

注意看打印出的Son对象地址的两个值相同:0x600000202140,类地址(Son的Class对象)的两个值也相同: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

猜你喜欢

转载自blog.csdn.net/wuyujin1997/article/details/129251529