Existing two classes:
1.object001 inherits from NSObject
#import <Foundation/Foundation.h>
@interface Object001:nsobject
Object001 header file and I'm just declaring a method here
-(void) printfstring;
@end
#import "Object001.h"
@implementation Object001
Object001 implementation file, I implemented the declared Printfstring method, which is used to print Object001 strings on the console
-(void) printfstring
{
NSLog (@ "Object001");
}
@end
2.object002 inherits from Object001
#import "Object001.h"
@interface object002:object001
No modification
@end
#import "Object002.h"
@implementation Object002
-(void) printfstring
{
[Super printfstring]; Object002 objects do not call methods in the Object002 parent class first
NSLog (@ "Object002");
}
@end
#import "ViewController.h"
#import "Object002.h"
@implementation Viewcontroller
Viewcontroller implementation
-(void) Viewdidload {
[Super Viewdidload];
Do no additional setup after loading the view, typically from a nib.
Object002 *object002 = [Object002 new];
[Object002 printfstring];
}
The console prints the following information:
Then the method in the parent class is invoked in the Object002 class to run again, and the console prints the following information:
By contrast, you can tell by overriding the method in the parent class in the subclass, without calling the method in the parent class, the method in the parent class is not executed, and the method in the parent class is overwritten as if it were rewritten with a name.
For example: In the following two very common methods, if a method in the parent class is not invoked without a parent class pointer, it can also run successfully, except that the object has less behavior, so when overriding a method in the parent class, you must first call the method in the parent class with the parent class pointer (super).
-(void) viewdidload {
[super Viewdidload];
//Does any additional Setu p after loading the view, typically from a nib.
}
-(void) Viewwillappear: (BOOL) animated
{
[super Viewwillappear: Animated];
}