当前位置:网站首页>关于静态类型、动态类型、id、instancetype
关于静态类型、动态类型、id、instancetype
2022-07-06 11:02:00 【InfoQ】
静态类型
- 一个指针变量定义为特定的对象时,使用的是静态类型,在编译的时候就知道这个指针变量所属的类,这个变量总是存储特定类的对象,默认情况下所有的数据类型都是静态数据类型
@interface Person : NSObject
property (nonatomic,strong) NSString *name;
-(void)run;
@end
@implementation Person
-(void)run{
NSLog(@"run");
}
@end
int main(int argc, const char * argv[]) {
Person *p = [[Person alloc]init];
p.name = @"ABC";
[p run];
return 0;
}
- 静态数据类型的特点:
- 在编译的时候就知道变量的类型
编译时就知道p是Person类型
- 知道变量中有哪些属性和方法
@property (nonatomic,strong) NSString *name;
-(void)run;
- 在编译的时候就可以访问这些属性和方法
p.name = @"ABC";
[p run];
- 并且如果是通过静态数据类型定义变量,如果访问了不属于静态数据类型的属性和方法,那么编译器就会报错

动态类型
- 指程序直到执行时才确定对象所属的类
@interface Person : NSObject
@property (nonatomic,strong) NSString *name;
-(void)run;
@end
@implementation Person
-(void)run{
NSLog(@"run");
}
-(void)test{
NSLog(@"test");
}
@end
@interface Student : Person
@property (nonatomic,assign) int age;
-(void)eat;
@end
@implementation Student
-(void)eat{
NSLog(@"eat");
}
@end
int main(int argc, const char * argv[]) {
id obj1 = [[Person alloc]init];
[obj1 run];
id obj2 = [[Student alloc]init];
[obj2 eat];
return 0;
}
- 动态数据类型的特点:
- 在编译的时候编译器并不知道变量的真实类型,只有在运行的时候才知道它的真实类型
id obj1 = [[Person alloc]init];
id obj2 = [[Student alloc]init];
id
类型与
instancetype
id
类型
id
是一种通用的对象类型,它可以指向属于任何类的对象,可理解为万用指针,相当于C语言的void*
数据类型,id
也是一个动态数据类型,可以用来定义变量,作为函数参数,作为函数返回值等等
id == NSObject*
(万能指针)
NSObject*
是一个静态类型
- 由于动态数据类型可以调用任意方法,所以有可能调用到不属于自己的方法,而编译时又不报错,所以导致运行时错误
- 使用场景:多态,可以减少代码量,避免调用子类特有的方法需要强制类型转化
- id类型不能使用点语法,因为点语法是编译器的特性,而id类型是运行时的特性
- 尽量使用静态类型,静态类型可以更早的发现错误,提高可读性
instancetype
instancetype
在编译时就可以判断对象的真实类型
instancetype
只能用于返回值
- 自定义的构造方法,返回值尽量使用
instancetype
,不要用id
边栏推荐
- 二叉搜索树
- Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
- If you have any problems, you can contact me. A rookie ~
- CSRF漏洞分析
- 使用cpolar建立一个商业网站(1)
- 线代笔记....
- POJ 2208 已知边四面体六个长度,计算体积
- 使用block实现两个页面之间的传统价值观
- Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
- 复现Thinkphp 2.x 任意代码执行漏洞
猜你喜欢
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
Use cpolar to build a business website (1)
AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
Deep circulation network long-term blood pressure prediction [translation]
美庐生物IPO被终止:年营收3.85亿 陈林为实控人
Execution process of MySQL query request - underlying principle
MySQL查询请求的执行过程——底层原理
CSRF vulnerability analysis
Medical image segmentation
爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
随机推荐
Binary search tree
RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
helm部署etcd集群
一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
2022/02/12
Docker安装Redis
287. Find duplicates
AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
Some understandings of tree LSTM and DGL code implementation
Atcoder a mountaineer
[depth first search] Ji suanke: a joke of replacement
随着MapReduce job实现去加重,多种输出文件夹
AcWing 3537.树查找 完全二叉树
How does crmeb mall system help marketing?
朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
使用block实现两个页面之间的传统价值观
Implementation of AVL tree
上海部分招工市場對新冠陽性康複者拒絕招錄
Collection of penetration test information -- use with nmap and other tools