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

    输出结果:

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

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

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

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

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

  • 相关阅读:
    sql注入-原理&防御
    vulnhub靶场之AI-WEB1.0渗透记录
    python -m http.server 搭建一个简易web下载服务器
    渗透测试工具-sqlmap
    OSI七层模型
    LAXCUS大数据操作系统3.03版本发布,欢迎使用试用
    松耦合和紧耦合的架构设计、性能对比
    Laxcus大数据操作系统2.0(5)- 第二章 数据组织
    从AlphaGo谈通用型人工智能设计
    基于深度推理、自编程、大数据的通用人工智能
  • 原文地址:https://www.cnblogs.com/zyc-it/p/3201271.html
Copyright © 2011-2022 走看看