在不同语言中static的用法
-
static
(计算机高级语言)
C++中
面向过程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//Example1 #include <iostream> using namespace std; void fn(); //声明函数 static int n; //声明静态全局变量 int main() { n = 20; //为n赋初值 printf ( "%d" , n); //输出n的值 fn(); //调用fn函数 } void fn() { n++; //n的值自加一(n=n+1) printf ( "%d" , n); //输出n的值 } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//Example2 //File1第一个代码文件的代码 #include <iostream> void fn(); //声明fn函数 static int n; //定义静态全局变量 int main() { n = 20; cout<<n<<endl; fn(); } //File2第二个代码文件的代码 #include <iostream.h> extern int n; void fn() { n++; printf ( "%d" , n); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//Example3 #include <iostream> #include <stdio.h> void fn(); int main() { fn(); fn(); fn(); } void fn() { static int n = 10; printf ( "%d" , n); n++; } |
1
2
3
4
5
6
7
8
9
10
11
12
|
//Example4 #include <iostream> static void fn(); //声明静态函数 int main() { fn(); } void fn() //定义静态函数 { int n = 10; printf ( "%d" , n); } |
面向对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
//Example5 #include <iostream> using namespace std; class Myclass { public : Myclass( int a, int b, int c); void GetSum(); private : int a; int b; int c; static int Sum; //声明静态数据成员 }; int Myclass::Sum = 0; //定义并初始化静态数据成员 Myclass::Myclass( int a, int b, int c) { this ->a = a; this ->b = b; this ->c = c; Sum += a + b + c; } void Myclass::GetSum() { cout<< "Sum=" <<Sum<<endl; } int main() { Myclass M(1, 2, 3); M.GetSum(); Myclass N(4, 5, 6); N.GetSum(); M.GetSum(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
//Example 6 #include <iostream> using namespace std; class Myclass { public : Myclass( int a, int b, int c); static void GetSum(); // 声明静态成员函数 private : int a, b, c; static int Sum; //声明静态数据成员 }; int Myclass::Sum=0; //定义并初始化静态数据成员 Myclass::Myclass( int a, int b, int c) { this ->a = a; this ->b = b; this ->c = c; Sum += a + b + c; //非静态成员函数可以访问静态数据成员 } void Myclass::GetSum() //静态成员函数的实现 { // cout<<a<<endl; //错误代码,a是非静态数据成员 cout<< "Sum=" <<Sum<<endl; } int main() { Myclass M(1, 2, 3); M.GetSum(); Myclass N(4, 5, 6); N.GetSum(); Myclass::GetSum(); } |
C语言中
1
2
3
4
5
6
7
|
int main( void ) { extern char a; // extern variable must be declared before use printf ( "%c " , a); ( void )msg(); return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <stdio.h> int fun( void ) { static int count = 10; // 此语句只在函数第一次调用时执行,后续函数调用此变量的初始值为上次调用后的值,每次调用后存储空间不释放 return count--; }, int count = 1; int main( void ) { printf ( "global\t\tlocal static\n" ); for (; count <= 10; ++count) printf ( "%d\t\t%d\n" , count, fun()); return 0; } |
1
2
3
4
5
6
7
8
|
#include <stdio.h> int a; int main( void ) { int i; static char str[10]; printf ( "integer: %d; string: (begin)%s(end)" , a, str); return 0; } |
JAVA语言中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// Demonstrate static variables,methods,and blocks. class UseStatic{ static int a = 3 ; static int b; static void meth( int x) { System.out.println( "x = " + x); System.out.println( "a = " + a); System.out.println( "b = " + b); } static { System.out.println( "Static block initialized." ); b = a * 4 ; } public static void main(String args[]) { meth( 42 ); } } |
1
2
3
4
5
6
7
|
static class CompanyEmployee{ public static string GetCompanyName(string name) { ... } public static string GetCompanyAddress(string address) { ... } } |
1
|
String M_string1 =CompanyEmployee.GetCompanyName(M_string2) |
1
2
3
4
5
6
7
8
|
static class CompanyEmployee{ //静态类 public string GetCompanyName(string name) { ... } //没有Static public static string GetCompanyAddress(string address) { ... } } CompanyEmployee M_CompE = new CompanyEmployee(); String M_string1 =M_CompE.GetCompanyName(M_string2); //直接引用 |
C#语言中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyNamespace { class Program { public static void Main( string [] args) { Console.WriteLine( "返回数组的和" ); int [] values = { 1,2,3,4,5}; int sum = Sum(values); Console.WriteLine(sum); } //计算数组的和 static int Sum( int [] valuesValue) { int sum = 0; for ( int i = 0; i < valuesValue.Length; i++) { sum = sum + valuesValue[i]; } return sum; } } } |
- 参考资料
-
- 1.Stephen Prata.C++ Primer Plus 5th:sams,2005
- 2.C++中的static怎么用 .百度文库.2017-10-09[引用日期2017-10-17]
在不同语言中static的用法的更多相关文章
- c语言中static的用法,包括全局变量和局部变量用static修饰
一.c程序存储空间布局 C程序一直由下列部分组成: 1)正文段--CPU执行的机器指令部分:一个程序只有一个副本:只读,防止程序由于意外事故而修改自身指令: 2)初始化数据段(数据段)--在程序中所有 ...
- c语言中static的用法
1.static定义变量: 1).局部: a.静态局部变量在函数内部定义,生存期为整个源代码,但作用域与自动变量相同,只能在定义的函数里面使用.退出该函数后,虽然此变量还存在内存中,但不能使用. b. ...
- c语言中static关键字用法详解
个人总结: 1.C不是面向对象的,在c中static修饰的变量或函数仅在当前文件中使用 2.C可以对局部变量使用static修饰(注意面向对象的java则不行),其放在全局区一直存在 概述static ...
- C语言中static用法介绍
C语言中static用法介绍 对于新手来说,很多东西的用法还不是很清楚,我们今天一起来看看C语言中static用法介绍 1.声明了static的变量称为静态变量,根据作用域的不同又分为 ...
- C语言中static的作用及C语言中使用静态函数有何好处
转自:http://www.jb51.net/article/74830.htm 在C语言中,static的作用有三条:一是隐藏功能,二是保持持久性功能,三是默认初始化为0. 在C语言中,static ...
- C语言中qsort函数用法
C语言中qsort函数用法-示例分析 本文实例汇总介绍了C语言中qsort函数用法,包括针对各种数据类型参数的排序,非常具有实用价值非常具有实用价值. 分享给大家供大家参考.C语言中的qsort ...
- C语言中static的使用方法【转】
本文转自:http://blog.csdn.net/renren900207/article/details/21609649 全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量 ...
- c语言中static 函数和普通函数的区别
C程序一直由下列部分组成: 1)正文段——CPU执行的机器指令部分:一个程序只有一个副本:只读,防止程序由于意外事故而修改自身指令: 2)初始化数据段(数据段)——在程序中所有赋了初值的全局变量,存放 ...
- C语言中sizeof()的用法
语法 sizeof有三种语法形式: 1.sizeof(object); //sizeof(对象); 2.sizeof(type_name); //sizeof(类型); 3.sizeof object ...
随机推荐
- 推荐系统模型之 FM
什么是FM模型 FM英文全称是“Factorization Machine”,简称FM模型,中文名“因子分解机”. FM模型其实有些年头了,是2010年由Rendle提出的,但是真正在各大厂大规模在C ...
- VPS、虚拟主机、云主机的区别
引用知乎网友通俗的例子解释: 1. 小王是深圳的一拆迁户,有钱任性:自己租了一块地皮[带宽],盖了一栋10000平方的房子[服务器],计划租给别人赚钱.2. 但是10000平方的房子太大,能租起的人太 ...
- 【Spark深入学习 -14】Spark应用经验与程序调优
----本节内容------- 1.遗留问题解答 2.Spark调优初体验 2.1 利用WebUI分析程序瓶颈 2.2 设置合适的资源 2.3 调整任务的并发度 2.4 修改存储格式 3.Spark调 ...
- ListFiles():返回Files类型数组,可以用getName()来访问到文件名。
List():显示文件的名(相对路径) ListFiles():返回Files类型数组,可以用getName()来访问到文件名. 使用isDirectory()和isFile()来判断究竟是文件还是目 ...
- iOS 之地图坐标体系和转换
一.坐标体系 首先我们要明白,开发者能接触到哪些坐标体系呢? 第一种分类: 1. GPS,WGS-84,原始坐标体系.一般用国际标准的GPS记录仪记录下来的坐标, 都是GPS的坐标.很可惜,在中国,任 ...
- GoLang之错误处理
错误处理 error Go语言引入了一个错误处理的标准模式,即error接口,该接口定义如下: type error interface { Error() string } 对于大多数函数,如果要返 ...
- opencv利用Cascade Classifier训练人脸检测器
opencv默认提供了haar特征和lbp特征训练的人脸分类器,但是效果不太好,所以我们可以用opencv提供的跑opencv_traincascade函数来训练一个LBP特征的分类器.(由于open ...
- python str byte 转换
# bytes object b = b"example" # str object s = "example" # str to bytes bytes(s, ...
- win7 win10开启网络访问(网络访问 无法访问 网络访问需要输入密码 等问题处理)
狂客原创,转载请注明.侵权必究! 右键单击桌面的“网络”图标 选择“属性”. 在弹出的“网络和共享中心”窗口,点击“更改高级共享设置”. 参考文章:https://jingyan.baidu.com/ ...
- Tensorflow一些常用基本概念与函数
1.tensorflow的基本运作 为了快速的熟悉TensorFlow编程,下面从一段简单的代码开始: import tensorflow as tf #定义‘符号’变量,也称为占位符 a = tf. ...