“概念”静态的在编程 [英] Concept of " Static " in Programming

查看:52
本文介绍了“概念”静态的在编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多次读过静态这个词。在某个地方,它是一个静态类。静态函数和静态变量甚至是静态数组。这个单词在编程中的实际概念是什么?

I've read many times the word static. somewhere it is a Static Class. Static Function and Static Variable even a static array. what is the actual concept of this word in programming?

推荐答案

粗略地说,在许多OOP语言中, static 限定符用于类方法,即您可以在没有类实例的情况下调用的方法(技术上它们类似于 C 函数,是没有隐藏的这个参数的方法。



但是,如果你真的想知道不同的含义(注意:即使在单一编程语言的上下文中也有不同的含义,例如,考虑 C ++ static 限定符然后你必须选择编程语言并研究它的参考手册。
Roughly speaking, in many OOP languages the static qualifier is used for class methods, that is methods you may call without having a instance of the class (technically they are similar to C functions, that is methods without the hidden this parameter).

However, if you really want to know exactly the different meanings (pay attention: different meanings even in the context of single programming language, consider, for instance, C++) of the static qualifier then you have to choose the programming language and study its reference manual.


根据标准C ++,static指的是一个对象的存储持续时间。因此,存储持续时间是对象的属性,它定义了存储内容应具有的生命周期。在C ++中有四个存储持续时间:

•静态存储持续时间

•线程存储持续时间

•自动存储持续时间

•动态存储持续时间



构建对象时指定存储持续时间。例如



static const int cte_LicId = 123455;



因此,此对象的存储应该持续程序运行的整个时间。通常,如果变量没有动态存储持续时间,没有线程存储持续时间,并且

不是局部变量,那么它具有静态存储持续时间。这样一个实体的存储将持续一段时间(即全局变量具有静态存储持续时间,即使它未在其声明中明确说明)



In在上面的例子中,静态存储持续时间与声明引入的对象cte_LicId相关联。因此,此对象的存储应该持续程序运行的整个时间。通常,如果变量没有动态存储持续时间,没有线程存储持续时间,并且不是局部变量,那么它具有静态存储持续时间。这样一个实体的存储应该持续到程序的持续时间(即全局变量具有静态存储持续时间,即使没有明确指定)





可以使用关键字static声明局部变量的静态存储持续时间。



int foo(int i){

static int s = rand(125);

返回i +1;

}



类似于类数据成员。有关详细信息,请参阅C ++参考指南。
According to the standard C++, static refers to the storage duration of an object. The storage duration is therefore a property of an object, and it defines the lifetime the contents of the storage should have. In C++ there are four storage durations:
• static storage duration
• thread storage duration
• automatic storage duration
• dynamic storage duration

A storage duration is specified when an object is constructed. For example

static const int cte_LicId = 123455;

As such the storage of this object should last the entire time the program runs. In general, if a variable does not have dynamic storage duration, does not have thread storage duration, and is
not a local variable then it has a static storage duration. The storage for such an entity will last for the duration of the program (i.e. a global variable has static storage duration even if it not explicitly stated in his declaration)

In the above example, the static storage duration is associated with the object, cte_LicId, introduced by the declaration. As such the storage of this object should last the entire time of the program's run. In general, if a variable does not have dynamic storage duration, does not have thread storage duration, and is not a local variable then it has a static storage duration. The storage for such an entity shall last for the duration of the program (i.e. a global variable has static storage duration even if it not explicitly specified)


A local variable can be declared with static storage duration using the keyword static.

int foo(int i) {
static int s = rand(125);
return i+1;
}

Similarly for a class data member. Please refer to your C++ reference guide for more info.


所有语言都没有通用概念。最相似的是变量。

下表以一种简单的方式介绍了C,C ++和C#。你看到的是静态的各种用途。



语言 静态文件级变量 静态函数级变量 静态文件级函数 静态类 静态成员变量 静态成员函数 静态构造函数
C 内部联系,生活在声明点直到程序结束 没有联系,从声明点开始直到程序结束 内部联系 n / a N / A N / A N / A
C ++ < td>与C 相同,与C 相同,C n / a 在类的所有实例之间共享 没有实例函数,没有隐藏* this *参数 不适用
C# n / a n / a n / a 类只包含静态字段(成员变量)和方法(成员函数) 与C ++相同 相同在从外部访问类的第一个元素之前调用C ++


干杯

Andi
There is no common concept in all the languages. The most similarity is with variables.
The table below covers C, C++ and C# in a maybe a bit simplistic way. What you see is the various uses of static.

Languagestatic file level variablestatic function level variablestatic file level functionstatic classstatic member variablestatic member functionstatic constructor
Cinternal linkage, lives from the point of declaration until the end of the programno linkage, lives from the point of declaration until the end of the programinternal linkagen/an/an/an/a
C++same as Csame as Csame as Cn/ashared between all instances of the classno instance function, has no hidden *this* parametern/a
C#n/an/an/aclass with only static fields (member variables) and methods (member functions)same as C++same as C++called before the first element of the class is accessed from outside

Cheers
Andi


这篇关于“概念”静态的在编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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