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

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

问题描述

在接口的实现中定义时,我不太了解静态变量.在方法中,我确实了解它们与局部变量的区别,但在直接在实现中定义时却没有.

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天全站免登陆