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);

    

}

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

posted @   brave-sailor  阅读(382)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2016-11-24 Android 开发中 iBeacon的使用
2016-11-24 Android与IOS的UUID的区别
2016-11-24 android蓝牙4.0(BLE)开发之ibeacon初步
2016-11-24 Android 开发中 iBeacon的使用
2016-11-24 android setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别
2015-11-24 Swift 闭包表达式
2015-11-24 swift 深入理解Swift的闭包
点击右上角即可分享
微信分享提示