接口实现中静态变量的含义是什么? [英] What's the meaning of static variables in an implementation of an interface?

查看:35
本文介绍了接口实现中静态变量的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解在接口的实现中定义的静态变量.在方法中,我确实理解它们与局部变量有何不同,但在实现中直接定义时则不然.

I don't quite understand static variables when defined in the implementation of an interface. In methods I do understand how they differ from local variables, but not when defined directly in an implementation.

看看这些例子.这两者实际上有什么区别?

Look at these examples. What difference do these two make practically?

#include "MyClass.h"

@implementation MyClass
int myInt;
...
@end

还有:

#include "MyClass.h"

@implementation MyClass
static int myInt;
...
@end

myInt 在这两种情况下对所有方法都是可见的,如果我解释了一个我正确运行的测试,myInt 在这两种情况下对于不同的实例都是相同的变量班级.

myInt is in both cases visible to all the methods, and if I interpreted a test I ran correctly, myInt will in both cases be the same variable for different instances of the class.

推荐答案

该上下文中的 'static' 关键字与普通 C 中的关键字相同:它将 myInt 的范围限制为当前文件.

The 'static' keyword in that context is the same as it would be in plain C: it limits the scope of myInt to the current file.

这篇关于接口实现中静态变量的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆