当前位置:网站首页>关于静态类型、动态类型、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
边栏推荐
- openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
- 监控界的最强王者,没有之一!
- 被疫情占据的上半年,你还好么?| 2022年中总结
- 深度循环网络长期血压预测【翻译】
- 2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
- The role of applet in industrial Internet
- Automatic reservation of air tickets in C language
- How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
- 使用block实现两个页面之间的传统价值观
- 44 colleges and universities were selected! Publicity of distributed intelligent computing project list
猜你喜欢

Tree-LSTM的一些理解以及DGL代码实现

Medical image segmentation

287. 寻找重复数

How are you in the first half of the year occupied by the epidemic| Mid 2022 summary

Splay

287. Find duplicates

About NPM install error 1

None of the strongest kings in the monitoring industry!
![[depth first search] Ji suanke: a joke of replacement](/img/f9/10dbbc2f6fed2095d2b155ecf87b75.jpg)
[depth first search] Ji suanke: a joke of replacement

测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
随机推荐
C#/VB.NET 给PDF文档添加文本/图像水印
Easy to use PDF to SVG program
test about BinaryTree
上海部分招工市场对新冠阳性康复者拒绝招录
Solve DoS attack production cases
Use map function and split function to type multiple elements in one line
Crawling data encounters single point login problem
How does crmeb mall system help marketing?
Penetration test information collection - App information
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
Oracle advanced (IV) table connection explanation
AvL树的实现
echart简单组件封装
十、进程管理
Hongke shares | plate by plate ar application in Beijing Winter Olympics
AFNetworking框架_上传文件或图像server
徐翔妻子应莹回应“股评”:自己写的!
There is a sound prompt when inserting a USB flash disk under win10 system, but the drive letter is not displayed
首先看K一个难看的数字