当前位置:网站首页>OC中new和init的区别
OC中new和init的区别
2022-08-02 02:23:00 【~废弃回忆 �༄】
OC中new和init的区别
我们重写一下一个新类的init方法:
- (instancetype)init {
self = [super init];
if (self != nil) {
NSLog(@"1");
}
return self;
}
- (instancetype)initWithName {
self = [super init];
if (self != nil) {
NSLog(@"2");
}
return self;
}
主函数:
Person *per = [[Person alloc] initWithName];
Person *per2 = [Person new];
输出:
2
1
new调用只会走1。
结论:
1.子类没有重写 init方法, new调用父类 init 方法
2.子类重写 init方法, new调用子类 init 方法
3.子类重写 init方法并自定义init方法 new的话不会调用,所以alloc + init更为灵活一些,扩展性好
边栏推荐
- leetcode / anagram in string - some permutation of s1 string is a substring of s2
- canal同步Mariadb到Mysql
- 29. 删除链表中重复的节点
- Electronic Manufacturing Warehouse Barcode Management System Solution
- 【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
- 线程的不同状态
- ofstream,ifstream,fstream read and write files
- Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
- Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
- Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
猜你喜欢

"NetEase Internship" Weekly Diary (1)

AI目标分割能力,无需绿幕即可实现快速视频抠图

Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products

MySQL8 download, start, configure, verify

2022河南青训联赛第(三)场

AI target segmentation capability for fast video cutout without green screen
![[LeetCode Daily Question]——654. The largest binary tree](/img/05/0af1c6dc0085e253c0758c8da63e52.png)
[LeetCode Daily Question]——654. The largest binary tree

nacos启动报错,已配置数据库,单机启动

oracle query scan full table and walk index

Golang分布式应用之定时任务
随机推荐
面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
openGauss切换后state状态显示不对
【LeetCode每日一题】——704.二分查找
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
GTK RGB图像绘制
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
Oracle数据类型介绍
Centos7 安装postgresql并开启远程访问
Rasa 3.x 学习系列- Rasa - Issues 4873 dispatcher.utter_message 学习笔记
Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
IMU预积分的简单理解
Golang分布式应用之Redis
软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
Ringtone 1161. Maximum In-Layer Elements and
AI目标分割能力,无需绿幕即可实现快速视频抠图
Remember a pit for gorm initialization
Oracle19c安装图文教程
[ORB_SLAM2] SetPose, UpdatePoseMatrices
Redis for distributed applications in Golang