zoukankan      html  css  js  c++  java
  • 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 + "	son0:"+son0.str0+"	son1:"+son1.str0);
                //Console.WriteLine("father:" + father.str1 + "	son0:" + son0.str1 + "	son1:" + son1.str1);
                //father parent=new father();
                //Console.WriteLine("father:" + father.str0 + "	son0:" + son0.str + "	son1:" + son1.str);
                //son0 children0= new son0();
                Console.WriteLine("father:" + father.str0 + "	son0:" + son0.str0 + "	son1:" + son1.str0);
                Console.WriteLine("father:" + father.str1 + "	son0:" + son0.str1 + "	son1:" + son1.str1);
                //son1 children1 = new son1();
                //Console.WriteLine("father:" + father.str0 + "	son0:" + son0.str0 + "	son1:" + 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 + "	son0:"+son0.str0+"	son1:"+son1.str0);
                //Console.WriteLine("father:" + father.str1 + "	son0:" + son0.str1 + "	son1:" + son1.str1);
                //father parent=new father();
                //Console.WriteLine("father:" + father.str0 + "	son0:" + son0.str + "	son1:" + son1.str);
                //son0 children0= new son0();
                Console.WriteLine("father:" + father.str0 + "	son0:" + son0.str0 + "	son1:" + son1.str0);
                Console.WriteLine("father:" + father.str1 + "	son0:" + son0.str1 + "	son1:" + son1.str1);
                //son1 children1 = new son1();
                //Console.WriteLine("father:" + father.str0 + "	son0:" + son0.str0 + "	son1:" + 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";
            }
        }
    }

    输出结果:

    通过这些简单的测试函数 我得到的结论是

    第一:在子类的静态字段中,如果这个字段从父类而来,当使用它的时候,不会唤醒子类的静态构造函数

    如果这个字段是它自己定义的静态字段,当使用的时候,就会唤醒子类的静态构造函数,可以用一句简单的话来概括

    静态字段在所声明的类中 ”全局 “唯一

    第二:子类静态构造函数会先唤醒父类静态构造函数

  • 相关阅读:
    2017-2018-2 《密码与安全新技术》课程总结
    2017-2018-2 《密码与安全新技术》论文总结
    2017-2018-2 20179226 《网络攻防》第14周作业
    2017-2018-2 《密码与安全新技术》第6周作业
    2017-2018-2 20179226 《网络攻防》第12周作业
    2017-2018-2 20179226 《网络攻防》第11周作业
    2017-2018-2 《密码与安全新技术》第5周作业
    2017-2018-2 20179226 《网络攻防》第10周作业
    2017-2018-2 《密码与安全新技术》第4周作业
    2017-2018-2 20179226 《网络攻防》第8周作业
  • 原文地址:https://www.cnblogs.com/zyc-it/p/3201271.html
Copyright © 2011-2022 走看看