当前位置:网站首页>Confusing basic concepts member variables local variables global variables
Confusing basic concepts member variables local variables global variables
2022-07-05 08:48:00 【InfoQ】
Member variables
@interface Person:NSObject
{
int age;
}
@end
- Variables written in braces of class declarations , We call it a member variable ( attribute 、 Instance variables )
- Member variables can only be accessed through objects
- Member variables cannot leave the class , After leaving the class, it is not a member variable
- Member variables cannot be initialized while being defined
- Storage : Pile up ( The storage space of the heap corresponding to the current object ). Data stored in the heap , Will not be released by the system , Only programmers can release
local variable
-(void)info{
int age = 0;
}
- Variables written in functions or code blocks , We call it a local variable
- Scope : Start with the line of definition , Until braces or
return
- Local variables can be defined first and then initialized , It can also be defined and initialized at the same time
- Storage : Stack . Data stored in the stack , The system will automatically release
Global variables
@implementation Person
int age = 0;
-(void)info{
}
@end
- Variables written outside functions and braces , It's called all variables
- Scope : Start with the line of definition , All the way to the end of the file
- Global variables can be defined first and then initialized , It can also be defined and initialized at the same time
- Storage : Static zone , As soon as the program starts, it allocates storage space , Not released until the end of the program
- There are two types of global variables :
static
Modify global variable , Available only in this source file
static int age =3;
@implementation Person
@end
- stay OC Declared in the syntax of `static` Static variables cannot be accessed directly through class names in other classes , Its scope can only be in the declared .m In file . However, you can call the method of this class to indirectly modify the value of this static variable
- `static` The variables declared by the keyword must be placed in `implementation` outside , Or in the method , If you don't assign a value to it , The default is 0, It initializes only once the program starts up ( It is not initialized after the class is instantiated )
- take static Static variables are written in methods , Its initialization is also when the program starts up , Once the program starts `static` Cannot be created in
extern
Modify global variable , Can be referenced by other classes .h The header file states
//.h In file
extern int age;
.m Implement the assignment in the file
//.m In file
#import "Person.h"
int age = 10;
@implementation Person
@end
extern
Can be placed before variables or functions , To represent the definition of a variable or function in another file , Prompt the compiler when it encounters this variable or function , Look for its definition in other modules . in addition ,extern
It can also be used to specify Links
- Be careful : Naming must be unique , To distinguish from global variables in other source files , If a global variable with the same name as another source file appears , It will report an error
- difference :
extern
Decorated global variables have external links by default , Scope is the whole project , Global variables defined in a file , In another file , adopt external Declaration of global variables , You can use global variables .
static
Decorated global static variables , The scope is the file where the variable is declared .
边栏推荐
- 2022.7.4-----leetcode. one thousand and two hundred
- Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
- Count of C # LINQ source code analysis
- 猜谜语啦(8)
- Apaas platform of TOP10 abroad
- The first week of summer vacation
- 猜谜语啦(4)
- Xrosstools tool installation for X-Series
- ROS learning 4 custom message
- [daily training] 1200 Minimum absolute difference
猜你喜欢
随机推荐
Guess riddles (5)
TypeScript手把手教程,简单易懂
Halcon Chinese character recognition
How apaas is applied in different organizational structures
kubeadm系列-02-kubelet的配置和启动
MATLAB小技巧(28)模糊綜合評價
Esphone Feixun DC1 soft change access homeassstant
Guess riddles (7)
Warning: retrying occurs during PIP installation
Arduino+a4988 control stepper motor
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
使用arm Neon操作,提高内存拷贝速度
猜谜语啦(7)
Affected tree (tree DP)
图解八道经典指针笔试题
287. Looking for repeats - fast and slow pointer
Programming implementation of subscriber node of ROS learning 3 subscriber
Solutions of ordinary differential equations (2) examples
Agile project management of project management
JS asynchronous error handling