当前位置:网站首页>关于静态类型、动态类型、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
边栏推荐
- Cobra quick start - designed for command line programs
- Docker installation redis
- Collection of penetration test information -- use with nmap and other tools
- Medical image segmentation
- POJ 2208 已知边四面体六个长度,计算体积
- Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
- Jdbc driver, c3p0, druid and jdbctemplate dependent jar packages
- bonecp使用数据源
- Hongke shares | plate by plate ar application in Beijing Winter Olympics
- If you have any problems, you can contact me. A rookie ~
猜你喜欢
![[Sun Yat sen University] information sharing of postgraduate entrance examination and re examination](/img/a8/41e62a7a8d0a2e901e06c751c30291.jpg)
[Sun Yat sen University] information sharing of postgraduate entrance examination and re examination

Use cpolar to build a business website (1)

openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度

【中山大学】考研初试复试资料分享

This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer

Docker installation redis
![Deep circulation network long-term blood pressure prediction [translation]](/img/9c/c1ed28242a4536c1e8fde3414f82a8.png)
Deep circulation network long-term blood pressure prediction [translation]

Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法

用于远程医疗的无创、无袖带血压测量【翻译】

Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
随机推荐
星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
Binary search tree
Bonecp uses data sources
AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
10、 Process management
A method of sequentially loading Unity Resources
Picture zoom Center
Penetration test information collection - App information
Some recruitment markets in Shanghai refuse to recruit patients with covid-19 positive
安装及管理程序
Online notes
十、进程管理
人体骨骼点检测:自顶向下(部分理论)
Automatic reservation of air tickets in C language
Markdown syntax for document editing (typera)
Introduction and case analysis of Prophet model
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability