当前位置:网站首页>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更为灵活一些,扩展性好
边栏推荐
- [Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
- 60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
- 【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
- MySQL8 download, start, configure, verify
- Golang分布式应用之定时任务
- PHP uses PHPRedis and Predis
- "NetEase Internship" Weekly Diary (1)
- 线程的不同状态
- BI-SQL丨WHILE
- [LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
猜你喜欢

Power button 1374. Generate each character string is an odd number

Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒

Remember a pit for gorm initialization

A good book for newcomers to the workplace

Remember a gorm transaction and debug to solve mysql deadlock

Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol

"NetEase Internship" Weekly Diary (3)

CASE2023

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

Handwritten Blog Platform ~ Day Two
随机推荐
to-be-read list
Redis 底层的数据结构
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
Remember a pit for gorm initialization
字符串常用方法
leetcode / anagram in string - some permutation of s1 string is a substring of s2
FOFAHUB usage test
The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
Centos7 安装postgresql并开启远程访问
nacos startup error, the database has been configured, stand-alone startup
ALCCIKERS Shane 20191114
Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
The failure to create a role in Dahua Westward Journey has been solved
swift项目,sqlcipher3 -> 4,无法打开旧版数据库有办法解决吗
canal同步Mariadb到Mysql
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
Safety (1)
AOF rewrite
oracle查询扫描全表和走索引