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";
            }
        }
    }

    输出结果:

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

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

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

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

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

  • 相关阅读:
    python note 30 断点续传
    python note 29 线程创建
    python note 28 socketserver
    python note 27 粘包
    python note 26 socket
    python note 25 约束
    Sed 用法
    python note 24 反射
    python note 23 组合
    python note 22 面向对象成员
  • 原文地址:https://www.cnblogs.com/zyc-it/p/3201271.html
Copyright © 2011-2022 走看看