当前位置:网站首页>容易混淆的基本概念 成员变量 局部变量 全局变量
容易混淆的基本概念 成员变量 局部变量 全局变量
2022-07-05 08:43:00 【InfoQ】
成员变量
@interface Person:NSObject
{
int age;
}
@end
- 写在类声明的大括号中的变量,我们称之为成员变量(属性、实例变量)
- 成员变量只能通过对象访问
- 成员变量不能离开类,离开类之后就不是成员变量
- 成员变量不能在定义的同时进行初始化
- 存储:堆(当前对象对应的堆的存储空间中)。存储在堆中的数据,不会被系统释放,只能程序员自己释放
局部变量
-(void)info{
int age = 0;
}
- 写在函数或者代码块中的变量,我们称之为局部变量
- 作用域:从定义的那一行开始,一直到大括号或者
return
- 局部变量可以先定义再初始化,也可以定义的同时初始化
- 存储:栈。存储在栈中的数据,系统给会自动释放
全局变量
@implementation Person
int age = 0;
-(void)info{
}
@end
- 写在函数和大括号外部的变量,称为全部变量
- 作用域:从定义的那一行开始,一直到文件末尾
- 全局变量可以先定义再初始化,也可以定义的同时初始化
- 存储:静态区,程序一启动就会分配存储空间,直到程序结束才会释放
- 全局变量分为两种:
static修饰全局变量,只在该源文件中可用
static int age =3;
@implementation Person
@end
- 在OC的语法中声明后的`static`静态变量在其他类中是不能通过类名直接访问的,它的作用域只能是在声明的这个.m文件中 。不过可以调用这个类的方法间接的修改这个静态变量的值
- `static`关键字声明的变量须放在`implementation`外面,或者方法中,如果不为它赋值,默认为0,它只在程序开机初始化一次(并不是该类实例化后才初始化的)
- 将static静态变量写在方法中,它的初始化也是在程序开机时,程序一旦启动以后`static`是不能在创建的
extern修饰全局变量,可以被其他类引用.h头文件中声明
//.h文件中
extern int age;
.m实现文件中赋值
//.m文件中
#import "Person.h"
int age = 10;
@implementation Person
@end
extern可置于变量或者函数前,以表示变量或者函数的定义在别的文件中,提示编译器遇到此变量或者函数时时,在其他模块中寻找其定义。另外,extern也可用来进行链接指定
- 注意:命名一定要很独特,才能区分与其他源文件中的全局变量,如果出现跟其他源文件同名的全局变量,则会报错误
- 区别:
extern修饰的全局变量默认是有外部链接的,作用域是整个工程,在一个文件内定义的全局变量,在另一个文件中,通过external全局变量的声明,就可以使用全局变量。
static修饰的全局静态变量,作用域是声明此变量所在的文件。
边栏推荐
- C语言标准函数scanf不安全的原因
- [daily training -- Tencent selected 50] 557 Reverse word III in string
- Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
- C language data type replacement
- Matlab tips (28) fuzzy comprehensive evaluation
- Halcon Chinese character recognition
- Pytorch entry record
- [daily training] 1200 Minimum absolute difference
- [nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
- Guess riddles (3)
猜你喜欢

Arduino+a4988 control stepper motor

Business modeling of software model | vision

Illustration of eight classic pointer written test questions

猜谜语啦(4)

It cold knowledge (updating ing~)

Sword finger offer 05 Replace spaces

Sword finger offer 06 Print linked list from end to end

猜谜语啦(3)

TypeScript手把手教程,简单易懂

How to manage the performance of R & D team?
随机推荐
第十八章 使用工作队列管理器(一)
每日一题——替换空格
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
Tips 1: Web video playback code
Agile project management of project management
Meta标签详解
leetcode - 445. Add two numbers II
整形的分类:short in long longlong
golang 基础 ——map、数组、切片 存放不同类型的数据
ABC#237 C
Arduino burning program and Arduino burning bootloader
每日一题——输入一个日期,输出它是该年的第几天
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
Arduino operation stm32
[牛客网刷题 Day4] JZ32 从上往下打印二叉树
[daily training] 1200 Minimum absolute difference
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Guess riddles (142)
Guess riddles (7)
One dimensional vector transpose point multiplication np dot
