当前位置:网站首页>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 .
边栏推荐
- It cold knowledge (updating ing~)
- kubeadm系列-02-kubelet的配置和启动
- Programming implementation of ROS learning 2 publisher node
- Halcon affine transformations to regions
- Shift operation of complement
- [daiy4] copy of JZ35 complex linked list
- 图解网络:什么是网关负载均衡协议GLBP?
- 容易混淆的基本概念 成员变量 局部变量 全局变量
- [牛客网刷题 Day4] JZ32 从上往下打印二叉树
- ROS learning 1- create workspaces and function packs
猜你喜欢
整形的分类:short in long longlong
猜谜语啦(11)
Low code platform | apaas platform construction analysis
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
Business modeling of software model | stakeholders
319. 灯泡开关
Guess riddles (11)
Guess riddles (9)
Halcon Chinese character recognition
MATLAB skills (28) Fuzzy Comprehensive Evaluation
随机推荐
Solutions of ordinary differential equations (2) examples
Illustration of eight classic pointer written test questions
Adaboost使用
12、动态链接库,dll
TF coordinate transformation of common components of ros-9 ROS
Use and programming method of ros-8 parameters
Basic number theory -- Euler function
Apaas platform of TOP10 abroad
Ros-10 roslaunch summary
MATLAB skills (28) Fuzzy Comprehensive Evaluation
Programming implementation of ROS learning 2 publisher node
图解八道经典指针笔试题
[牛客网刷题 Day4] JZ35 复杂链表的复制
Daily question - input a date and output the day of the year
ROS learning 1- create workspaces and function packs
TypeScript手把手教程,简单易懂
【日常训练】1200. 最小绝对差
Guess riddles (3)
Halcon blob analysis (ball.hdev)
ORACLE进阶(三)数据字典详解