c# 类的初始化顺序

     类的初始化顺序:

                 “如下图”


     代码如下所示:

    class Program
    {
        static void Main(string[] args)
        {
            Child child = new Child();
            Console.ReadKey();
        }
    }
    public class Father
    {
        public static string strA = methodFatherA();
        public string strB = methodFatherB();

        public static string methodFatherB()
        {
            Console.WriteLine("我是父类变量");
            return "我是父类变量";
        }
        public static string methodFatherA()
        {
            Console.WriteLine("我是父类静态变量");
            return "我是父类静态变量";
        }
        static Father()
        {
            Console.WriteLine("调用父类静态构造函数");
        }
        public Father()
        {
            Console.WriteLine("调用父类构造函数");
        }
    }

    public class Child : Father
    {
        public static string subStrA = methodA();
        public string subStrB = methodB();

        public static string methodB()
        {
            Console.WriteLine("我是子类变量");
            return "我是子类变量";
        }
        public static string methodA()
        {
            Console.WriteLine("我是子类静态变量");
            return "我是子类静态变量";
        }
        static Child()
        {
            Console.WriteLine("调用子类静态构造函数");
        }
        public Child()
        {
            Console.WriteLine("调用子类构造函数");
        }
    }




你可能感兴趣的:(C#)