当前位置:网站首页>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更为灵活一些,扩展性好
边栏推荐
- Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
- Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
- AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
- qt点云配准软件
- 拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
- Can Youxuan database import wrongly be restored?
- Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
- 789. 数的范围
- 53. 最小的k个数
- AntPathMatcher uses
猜你喜欢

oracle query scan full table and walk index

Speed up your programs with bitwise operations

LeetCode brush diary: LCP 03. Machine's adventure

Handwritten Blog Platform ~ Day Two

【web】理解 Cookie 和 Session 机制

接口测试神器Apifox究竟有多香?

Analysis of volatile principle

极大似然估计

使用docker安装mysql

The failure to create a role in Dahua Westward Journey has been solved
随机推荐
【 wheeled odometer 】
to-be-read list
"NetEase Internship" Weekly Diary (3)
记一个gorm初始化的坑
使用DBeaver进行mysql数据备份与恢复
790. 数的三次方根
qt点云配准软件
Hash collisions and consistent hashing
BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
Remember a pit for gorm initialization
Win Go development kit installation configuration, GoLand configuration
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
How to adjust the cross cursor too small, CAD dream drawing calculation skills
项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
swift项目,sqlcipher3 -> 4,无法打开旧版数据库有办法解决吗
Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
[ORB_SLAM2] SetPose, UpdatePoseMatrices
Outsourcing worked for three years, it was abolished...
工程师如何对待开源
Redis 底层的数据结构