当前位置:网站首页>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更为灵活一些,扩展性好
边栏推荐
- nacos startup error, the database has been configured, stand-alone startup
- Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
- ros多客户端请求服务
- From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
- Redis 底层的数据结构
- swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
- 记一次gorm事务及调试解决mysql死锁
- Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
- [Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
- 2022年NPDP考完多久出成绩?怎么查询?
猜你喜欢
随机推荐
【 wheeled odometer 】
Ringtone 1161. Maximum In-Layer Elements and
Centos7 安装postgresql并开启远程访问
数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
永磁同步电机36问(二)——机械量与电物理量如何转化?
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
Safety (1)
Scheduled tasks for distributed applications in Golang
CASE2023
nacos启动报错,已配置数据库,单机启动
CodeTon Round 2 D. Magical Array
2022-08-01 mysql/stoonedb慢SQL-Q18分析
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
Check if IP or port is blocked
nacos startup error, the database has been configured, stand-alone startup
2022 Henan Youth Training League Game (3)
EFCore 反向工程
oracle query scan full table and walk index