当前位置:网站首页>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更为灵活一些,扩展性好
边栏推荐
- Analysis of volatile principle
- NIO‘s Sword(牛客多校赛)
- "NetEase Internship" Weekly Diary (1)
- ofstream,ifstream,fstream read and write files
- 60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
- 项目后台技术Express
- [LeetCode Daily Question]——654. The largest binary tree
- Project Background Technology Express
- Golang分布式应用之Redis
- nacos startup error, the database has been configured, stand-alone startup
猜你喜欢
随机推荐
AI目标分割能力,无需绿幕即可实现快速视频抠图
Can Youxuan database import wrongly be restored?
Safety (1)
Safety (2)
FOFAHUB使用测试
oracle query scan full table and walk index
FOFAHUB usage test
Install mysql using docker
AOF rewrite
Redis for distributed applications in Golang
Speed up your programs with bitwise operations
PHP live source code to achieve simple barrage effect related code
Handwriting a blogging platform ~ Day 3
PHP uses PHPRedis and Predis
790. 数的三次方根
2022河南青训联赛第(三)场
The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
通用客户端架构
yaml
使用DBeaver进行mysql数据备份与恢复