当前位置:网站首页>About static type, dynamic type, ID, instancetype
About static type, dynamic type, ID, instancetype
2022-07-06 18:51:00 【InfoQ】
Static type
- When a pointer variable is defined as a specific object , Using static types , The class of this pointer variable is known at compile time , This variable always stores objects of a specific class , By default, all data types are static
@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;
}
- Characteristics of static data types :
- You know the type of variable at compile time
You know it at compile time p yes Person type
- Know which properties and methods are in the variable
@property (nonatomic,strong) NSString *name;
-(void)run;
- These properties and methods can be accessed at compile time
p.name = @"ABC";
[p run];
- And if you define variables through static data types , If you access properties and methods that are not static data types , Then the compiler will report an error
Dynamic type
- It refers to the class that the program does not determine the object belongs to until it is executed
@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;
}
- Characteristics of dynamic data types :
- At compile time, the compiler doesn't know the real type of the variable , The real type of it is known only at run time
id obj1 = [[Person alloc]init];
id obj2 = [[Student alloc]init];
id
The type and
instancetype
id
type
id
Is a common object type , It can point to objects belonging to any class , It can be understood as a universal pointer , amount to C Linguisticvoid*
data type ,id
It is also a dynamic data type , Can be used to define variables , As a function parameter , Return value as a function, etc
id == NSObject*
( Universal pointer )
NSObject*
It's a static type
- Because dynamic data types can call any method , So it's possible to call methods that don't belong to you , And no error is reported during compilation , So it leads to runtime errors
- Use scenarios : polymorphic , Can reduce the amount of code , To avoid calling subclass specific methods, you need to force type conversion
- id Type cannot use point syntax , Because point syntax is a feature of the compiler , and id Types are runtime characteristics
- Try to use static types , Static types can detect errors earlier , Improve readability
instancetype
instancetype
The true type of the object can be determined at compile time
instancetype
Can only be used for return values
- Custom construction method , Use the return value as much as possible
instancetype
, Do not useid
边栏推荐
- From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
- Echart simple component packaging
- 被疫情占据的上半年,你还好么?| 2022年中总结
- 2022/02/12
- QPushButton绑定快捷键的注意事项
- 线代笔记....
- Cobra quick start - designed for command line programs
- A wearable arm device for night and sleeveless blood pressure measurement [translation]
- Some understandings of tree LSTM and DGL code implementation
- 华为0基金会——图片整理
猜你喜欢
Numerical analysis: least squares and ridge regression (pytoch Implementation)
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
使用cpolar建立一个商业网站(1)
If you have any problems, you can contact me. A rookie ~
The role of applet in industrial Internet
Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
能源行业的数字化“新”运维
Oracle advanced (IV) table connection explanation
监控界的最强王者,没有之一!
随机推荐
DOM简要
QPushButton绑定快捷键的注意事项
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
Nuc11 cheetah Canyon setting U disk startup
Markdown syntax for document editing (typera)
POJ 2208 已知边四面体六个长度,计算体积
Easy to use PDF to SVG program
[depth first search] Ji suanke: find numbers
Medical image segmentation
手写一个的在线聊天系统(原理篇1)
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
atcoder它A Mountaineer
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
On AAE
A method of sequentially loading Unity Resources
Summary of performance knowledge points
C language college laboratory reservation registration system
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
青龙面板最近的库
Penetration test information collection - CDN bypass