日期:2014-05-20  浏览次数:20722 次

一段代码,关于static的
C# code
private void button1_Click(object sender, EventArgs e)
        {
            iambase am1 = new iambase();
            iambase am2 = new iambase();
            MessageBox.Show(iambase.count.ToString());
        }

C# code
public class iambase
        {
            public static int count = 0;

            public iambase()
            {
                count++;
            }

             static iambase()
            {
                count++;
            }
        }


我感觉是每一次new一个对象的时候count+1 为什么结果是3?谢谢解答


------解决方案--------------------
1 ->public static int count = 0;

2 -> static iambase()
{
count++;
}

3 -> am1 = new iambase();

public iambase()
{
count++;
}

4 -> am2 = new iambase();

public iambase()
{
count++;
}