当前位置:网站首页>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 .
边栏推荐
- 猜谜语啦(3)
- Halcon Chinese character recognition
- Cmder of win artifact
- Shift operation of complement
- Guess riddles (2)
- STM32 lights up the 1.8-inch screen under Arduino IDE
- ROS learning 4 custom message
- Hello everyone, welcome to my CSDN blog!
- Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
- ORACLE进阶(三)数据字典详解
猜你喜欢
Redis implements a high-performance full-text search engine -- redisearch
Guess riddles (10)
STM32 lights up the 1.8-inch screen under Arduino IDE
Use and programming method of ros-8 parameters
Guess riddles (7)
My university
猜谜语啦(142)
整形的分类:short in long longlong
Halcon: check of blob analysis_ Blister capsule detection
Programming implementation of ROS learning 2 publisher node
随机推荐
An enterprise information integration system
Shift operation of complement
js异步错误处理
location search 属性获取登录用户名
[牛客网刷题 Day4] JZ35 复杂链表的复制
My university
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
猜谜语啦(142)
C language data type replacement
Basic number theory -- Euler function
【日常训练】1200. 最小绝对差
Array,Date,String 对象方法
How to manage the performance of R & D team?
Meizu Bluetooth remote control temperature and humidity access homeassistant
696. 计数二进制子串
猜谜语啦(3)
Daily question - input a date and output the day of the year
Chapter 18 using work queue manager (1)
Latex improve
golang 基础 —— golang 向 mysql 插入的时间数据和本地时间不一致