当前位置:网站首页>关于静态类型、动态类型、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
边栏推荐
- 手写一个的在线聊天系统(原理篇1)
- Blue Bridge Cup real question: one question with clear code, master three codes
- 基于蝴蝶种类识别
- AcWing 3537.树查找 完全二叉树
- Automatic reservation of air tickets in C language
- 巨杉数据库首批入选金融信创解决方案!
- Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
- RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
- RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
- Method of accessing mobile phone storage location permission under non root condition
猜你喜欢

helm部署etcd集群
![[the 300th weekly match of leetcode]](/img/a7/16b491656863e2c423ff657ac6e9c5.png)
[the 300th weekly match of leetcode]

Penetration test information collection - CDN bypass

Numerical analysis: least squares and ridge regression (pytoch Implementation)

重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用

Describe the process of key exchange

Method of accessing mobile phone storage location permission under non root condition
![[Matlab] Simulink 同一模块的输入输出的变量不能同名](/img/99/adfe50075010916439cd053b8f04c7.png)
[Matlab] Simulink 同一模块的输入输出的变量不能同名

Nuc11 cheetah Canyon setting U disk startup

【LeetCode第 300 场周赛】
随机推荐
华为0基金会——图片整理
Huawei 0 foundation - image sorting
Oracle advanced (IV) table connection explanation
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
MySQL查询请求的执行过程——底层原理
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
CSRF漏洞分析
Stm32+esp8266+mqtt protocol connects onenet IOT platform
上海部分招工市場對新冠陽性康複者拒絕招錄
Solve DoS attack production cases
Docker安装Redis
wx小程序学习笔记day01
epoll()无论涉及wait队列分析
C language college laboratory reservation registration system
Penetration test information collection - site architecture and construction
A method of sequentially loading Unity Resources
Markdown syntax for document editing (typera)
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
抽象类与抽象方法
根据PPG估算血压利用频谱谱-时间深度神经网络【翻】