c# 类静态字段和静态构造函数->吃饭的你就别过来了,谨防胃溃疡
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 测试类型静态构造函数 { using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { string result = son0.str0; //Console.WriteLine("father:" + father.str0 + "\tson0:"+son0.str0+"\tson1:"+son1.str0); //Console.WriteLine("father:" + father.str1 + "\tson0:" + son0.str1 + "\tson1:" + son1.str1); //father parent=new father(); //Console.WriteLine("father:" + father.str0 + "\tson0:" + son0.str + "\tson1:" + son1.str); //son0 children0= new son0(); Console.WriteLine("father:" + father.str0 + "\tson0:" + son0.str0 + "\tson1:" + son1.str0); Console.WriteLine("father:" + father.str1 + "\tson0:" + son0.str1 + "\tson1:" + son1.str1); //son1 children1 = new son1(); //Console.WriteLine("father:" + father.str0 + "\tson0:" + son0.str0 + "\tson1:" + son1.str0); Console.Read(); } } public class father { public static string str0 = " father str0 静态字符串"; public static string str1 = "father str1 静态字符串"; static father() { Console.WriteLine("开始调用father的静态构造函数"); str0 = "father class"; // str1 = "father class"; } } public class son0:father { public static string strSon = "son0内部静态字符串"; static son0() { Console.WriteLine("开始调用son0的静态构造函数"); //str0 = "son0 class"; str1 = "son0 class"; } } public class son1:father { public static string strSon = "son1内部静态字符串"; static son1() { Console.WriteLine("开始调用都son1的静态构造函数"); str0= "son1 class"; } } }
输出结果:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 测试类型静态构造函数 { using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { string result = son0.strSon; //Console.WriteLine("father:" + father.str0 + "\tson0:"+son0.str0+"\tson1:"+son1.str0); //Console.WriteLine("father:" + father.str1 + "\tson0:" + son0.str1 + "\tson1:" + son1.str1); //father parent=new father(); //Console.WriteLine("father:" + father.str0 + "\tson0:" + son0.str + "\tson1:" + son1.str); //son0 children0= new son0(); Console.WriteLine("father:" + father.str0 + "\tson0:" + son0.str0 + "\tson1:" + son1.str0); Console.WriteLine("father:" + father.str1 + "\tson0:" + son0.str1 + "\tson1:" + son1.str1); //son1 children1 = new son1(); //Console.WriteLine("father:" + father.str0 + "\tson0:" + son0.str0 + "\tson1:" + son1.str0); Console.Read(); } } public class father { public static string str0 = " father str0 静态字符串"; public static string str1 = "father str1 静态字符串"; static father() { Console.WriteLine("开始调用father的静态构造函数"); str0 = "father class"; // str1 = "father class"; } } public class son0:father { public static string strSon = "son0内部静态字符串"; static son0() { Console.WriteLine("开始调用son0的静态构造函数"); //str0 = "son0 class"; str1 = "son0 class"; } } public class son1:father { public static string strSon = "son1内部静态字符串"; static son1() { Console.WriteLine("开始调用都son1的静态构造函数"); str0= "son1 class"; } } }
输出结果:
通过这些简单的测试函数 我得到的结论是
第一:在子类的静态字段中,如果这个字段从父类而来,当使用它的时候,不会唤醒子类的静态构造函数
如果这个字段是它自己定义的静态字段,当使用的时候,就会唤醒子类的静态构造函数,可以用一句简单的话来概括
静态字段在所声明的类中 ”全局 “唯一
第二:子类静态构造函数会先唤醒父类静态构造函数