目的:验证当类中有静态成员时,是不是多个实例公用
结论: 是的
- 首先是简单的类
public class ClsAA
{
static int kk = 0;
public void ValuePlus()
{
kk += 1;
}
public int GetValue()
{
return kk;
}
}
- 我在按钮事件里做的
private void button1_Click(object sender, EventArgs e)
{
CAA c1 = new CAA();
CAA c2 = new CAA();
Console.WriteLine(string.Format("c1={0},c2{1} ", c1.GetValue(), c2.GetValue()));
c1.ValuePlus();
Console.WriteLine(string.Format("c1={0},c2={1}", c1.GetValue(), c2.GetValue()));
c2.ValuePlus();
c2.ValuePlus();
Console.WriteLine(string.Format("c1={0},c2={1}", c1.GetValue(), c2.GetValue()));
}
很简单, 就是看一个实例里值变化会不会影响到另一个实例
结果如下
c1=0,c2=0
c1=1,c2=1
c1=3,c2=3