您的位置:首页 > 编程语言 > C#

c# 类的初始化顺序

2017-04-06 21:35 281 查看
     类的初始化顺序:

                 “如下图”

     代码如下所示:

    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("调用子类构造函数");

        }

    }



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: