The reason that iOS overrides a method in the parent class to call the method in the parent class first

Source: Internet
Author: User

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];
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.