当前位置:网站首页>容易混淆的基本概念 成员变量 局部变量 全局变量
容易混淆的基本概念 成员变量 局部变量 全局变量
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修饰的全局静态变量,作用域是声明此变量所在的文件。
边栏推荐
- 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
- Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
- 319. 灯泡开关
- Explore the authentication mechanism of StarUML
- Apaas platform of TOP10 abroad
- Halcon blob analysis (ball.hdev)
- How to write cover letter?
- 猜谜语啦(4)
- Bluebridge cup internet of things competition basic graphic tutorial - clock selection
- Halcon color recognition_ fuses. hdev:classify fuses by color
猜你喜欢

319. 灯泡开关

MATLAB小技巧(28)模糊綜合評價

Halcon affine transformations to regions

Xrosstools tool installation for X-Series

Business modeling | process of software model

Business modeling of software model | object modeling

Mathematical modeling: factor analysis
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?

How to manage the performance of R & D team?

Halcon color recognition_ fuses. hdev:classify fuses by color
随机推荐
319. 灯泡开关
Five design details of linear regulator
Infix expression evaluation
Halcon affine transformations to regions
One dimensional vector transpose point multiplication np dot
Digital analog 2: integer programming
剑指 Offer 09. 用两个栈实现队列
Redis实现高性能的全文搜索引擎---RediSearch
Halcon Chinese character recognition
JS asynchronous error handling
猜谜语啦(2)
Sword finger offer 05 Replace spaces
Adaboost使用
RT-Thread内核快速入门,内核实现与应用开发学习随笔记
Reasons for the insecurity of C language standard function scanf
Business modeling of software model | object modeling
Digital analog 1: linear programming
猜谜语啦(142)
Halcon blob analysis (ball.hdev)
MATLAB skills (28) Fuzzy Comprehensive Evaluation
