(C# 基础) 静态字段,静态类,静态方法。
静态字段被类的所有实例所共享,即此类的所有实例都访问同一内存地址。 所以该内存位置的值变更的话,这种变更对所有的实例都可见。
class MyClass
{
int number = ;
static int staticNumber = ; public void SetValue(int value1, int value2)
{
this.number = value1;
staticNumber = value2;
} public string DisplayValues()
{
return string.Format("number:{0}, staticNumber:{1}", this.number, staticNumber);
}
}
class Program
{
static void Main(string[] args)
{
MyClass class1 = new MyClass();
MyClass class2 = new MyClass(); class1.SetValue(, ); Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); class2.SetValue(, ); Console.WriteLine("Values in class2 are: {0}", class2.DisplayValues()); // 再次显示 class1 实例,发现 staticNumber 的值是 30.
Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues());
}
}
进一步,如果有多个线程同时访问静态字段,并对其赋值,那么会出现什么样的情况呢?
(由于进程是一组资源,而进程中的多个线程会共享进程中的资源。)
实际操作发现,对int 字段的访问非常快,不会出现资源抢夺问题。
public class Worker
{
// Volatile is used as hint to the compiler that this data member will be accessed by multiple threads.
private volatile bool shouldStop; private MyClass class3 = new MyClass(); // This method will be called when the thread is started.
public void DoWork()
{
while (!shouldStop)
{
Console.WriteLine("Worker thread: working and setting values.");
class3.SetValue(, );
} Console.WriteLine("Worker thread: terminating gracefully...");
} public void RequestStop()
{
this.shouldStop = true;
}
}
static void Main(string[] args)
{
MyClass class1 = new MyClass();
MyClass class2 = new MyClass(); class1.SetValue(, ); Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); class2.SetValue(, ); Console.WriteLine("Values in class2 are: {0}", class2.DisplayValues()); // 再次显示 class1 实例,发现 staticNumber 的值是 20.
Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); // Create the thread object.
Worker worker1 = new Worker();
Thread workerThread = new Thread(worker1.DoWork); // Start the worker thread.
workerThread.Start(); // Loop until worker thread acivates.
while (!workerThread.IsAlive) ; // Put the main thread to sleep for a while to allow worker thread do some work.
Thread.Sleep(); // Set values by main thread.
class1.SetValue(, );
Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); // Request the worker thread to stop.
worker1.RequestStop(); // Use the Join method to block the current thread until the object's thread terminates.
workerThread.Join();
Console.WriteLine("Main thread: Worker thread has terminated."); Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues());
}
如果静态字段是一个非托管资源,会怎么样呢?
(C# 基础) 静态字段,静态类,静态方法。的更多相关文章
- 零基础学Java(12)静态字段与静态方法
静态字段与静态方法 之前我们都定义的main方法都被标记了static修饰符,那到底是什么意思?下面我们来看看 静态字段 如果将一个字段定义为static,每个类只有一个这样的字段.而对于非静 ...
- C#学习笔记----静态字段和静态方法
1.使用关键字 static 修饰的字段或方法成为静态字段和静态方法,如 public static int num = 1;2.静态字段属于类,并为类所用.而非静态字段属于对象,只能被特定的对象专有 ...
- (10)C#静态方法,静态字段,静态类,匿名类
6.静态方法 使用静态方法就可不必用类的实例化调用次函数 class Test { public static void method() { ........ } //当调用一个method()时就 ...
- Python学习:16.Python面对对象(三、反射,构造方法,静态字段,静态方法)
一.构造方法 在使用类创建对象的时候(就是类后面加括号)就自动执行__init__方法. class A: def __init__(self): print('A') class B: def __ ...
- 深入理解 静态类和静态字段(C# 基础)
序言 以前,总是被提醒,在编程过程中尽量少用静态变量,数据丢失什么的,今天有空,禁不住对静态变量的强烈好奇,跟我一起了解下静态家族的内幕吧. 静态类 定义 静态类与非静态类的重要区别在于静态类不能实例 ...
- JAVA的静态变量、静态方法、静态类
静态变量和静态方法都属于静态对象,它与非静态对象的差别需要做个说明. (1)Java静态对象和非静态对象有什么区别? 比对如下: 静态对象 ...
- 关于Java的静态:静态类、静态方法、静态变量、静态块等
原文地址:Java static keyword - Class, Method, Variable, Block, import - JournalDev 很少看到文章能把静态这个问题解释的很清楚, ...
- 面向对象银角大王补充2-self就是调用当前方法的对象-静态字段,公有属性-封装的理解-继承的理解,普通方法,静态方法
self是什么,就是一个函数,就是一个形式参数 4.self就是调用当前方法的对象 静态字段,公有属性 静态字段使用场景,每个对象中保存相同的东西时,可以使用静态字段,公有属性 5.封装的理解 类中封 ...
- java静态方法和静态字段
public class Dog{ public static void main(String[]args){ A a= new A(); a.add(); //java实例对象可以访问类的静态方法 ...
随机推荐
- [USACO5.4]奶牛的电信Telecowmunication 最小割
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- 谁能赢呢? BZOJ 2463
题目描述 小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的格子之前不能被访问 ...
- window.open 设置高和宽无效
当设置_self属性时,再设置宽和高就不管用,这个宽高会继承父窗口的宽高! window.open("url","_self","width=100, ...
- Luogu P1608 路径统计 最短路计数
颓了...重边导致我乖乖用邻接矩阵.... 好吧就是个最短路计数....如果更新时d[v]==d[u]+w[i],就可以接起来,把两个加在一起.. 如果d[v]>d[u]+w[i],那么c[v] ...
- Socket 代码收藏
class SocketHelper { public delegate void Uplistbox(string txt); public Uplistbox u ...
- SVN更改通知的工具commitmonitor
SVN更改通知的工具commitmonitor 下载地址: https://sourceforge.net/projects/commitmonitor/files/latest/download 工 ...
- getElementsByTagName() 兼容性
写东西的时候用到了getElementsByTagName(),我一直以为js 写的东西是不会有兼容性的,这次在ie8下,getElementsByTagName()就不支持了,那怎么办呢,我就查到一 ...
- Excel中,用CONCATENATE函数生成SQL语句
1.语法 CONCATENATE(text1, [text2], ...)CONCATENATE 函数语法具有下列参数(参数为:操作.事件.方法.属性.函数或过程提供信息的值.):Text1 必需.要 ...
- input类型为file改变默认按钮样式
改变 input file 样式(input 文件域)是很多前端朋友经常遇到的头疼问题,今天推荐两种改变 input file 样式的两种常用方法: 方法一: <input type=&quo ...
- java && C# 线程
1.多个线程用到同一个资源的话,必须lock 2.为了解决,在竞争的情况下,优先分配资源给A.就是A和B线程都同时在同一时刻需要资源x,然后的话也不清楚系统是具体怎样调度的.或者说怎样调度,都有可 ...