iOS方法重写

在O-C中子类可以继承父类的方法 ,而不需要从新编写相同的方法,但是有有时候子类并不想原封不动的继承父类的方法,而且是想做一些修改,这就采用啦方法的重写,方法从写有叫做方法覆盖,若子类的中的方法与父类中的某一个方法具有相同的方法名,返回值类型和参数表,则新方法就会把原有的方法覆盖。

父类:

#import "MyViewController.h"


@interface MyViewController ()


@end


@implementation MyViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    [selfsetX];

// Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)setX

{

   int a = 10;

    NSLog(@"A类方法的值:%zi",a);

    

}

@end


子类:

#import "MyBclassViewController.h"


@interface MyBclassViewController ()


@end


@implementation MyBclassViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)setX

{

   int a = 100;

    NSLog(@"B类A的值:%zi",a);

    

}

方法 的重写 可以 写一些自己想要的控件 感觉挺实用的。

原文地址:https://www.cnblogs.com/Free-Thinker/p/7891230.html