静态类、静态方法、静态变量的区别

2022-06-09 15:38:09 浏览数 (1)

引言

一直疑惑静态类、静态方法、静态变量的内存驻用情况。今天就写了个Demo来深入八一八他们的区别和注意点。

为了演示方便,方法名和变量名采取中文命名

先上结论

静态变量 只在类初始化时加载一次

静态方法和静态方法语法糖 实时加载里面的内容

只读属性 实时加载里面的内容

附有初始值的属性语法糖 只在类初始化时加载一次(应该是set里面处理的,而不是get里面处理的)

静态有参方法 实时加载里面的内容

无图我还说什么? 请看代码

代码

先看测试Demo

1.非静态类 中静态方法

代码语言:javascript复制
    /// <summary>
    /// 非静态类
    /// </summary>
    public class TestCommonService
    {
        /// <summary>
        /// 静态变量
        /// </summary>
        public static string 静态变量 = ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");
        /// <summary>
        /// 静态方法
        /// </summary>
        /// <returns></returns>
        public static string 静态方法()
        {
            return ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");
        }
        /// <summary>
        /// 静态方法语法糖
        /// </summary>
        /// <returns></returns>
        public static string 静态方法语法糖() => ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");

        /// <summary>
        /// 只读属性
        /// </summary>
        public static string 只读属性 => ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");
        /// <summary>
        /// 属性语法糖
        /// </summary>
        public static string 属性语法糖 { get; set; } = ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");

        /// <summary>
        /// 属性
        /// </summary>
        public static string 标准属性
        {
            get
            {
                return ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");
            }
            set
            {
            }
        }


        /// <summary>
        /// 静态有参方法
        /// </summary>
        /// <param name="isValue"></param>
        /// <returns></returns>
        public static string 静态有参方法(bool isValue)
        {
            return isValue ? $"true:{ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "")}" : $"false:{ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "")}";
        }
    }

2.静态类静态方法

代码语言:javascript复制
/// <summary>
    /// 静态类
    /// </summary>
    public static class TestStaticService
    {
        /// <summary>
        /// 静态变量
        /// </summary>
        public static string 静态变量 = ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");
        /// <summary>
        /// 静态方法
        /// </summary>
        /// <returns></returns>
        public static string 静态方法()
        {
            return ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");
        }
        /// <summary>
        /// 静态方法语法糖
        /// </summary>
        /// <returns></returns>
        public static string 静态方法语法糖() => ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");

        /// <summary>
        /// 只读属性
        /// </summary>
        public static string 只读属性 => ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");
        /// <summary>
        /// 属性语法糖
        /// </summary>
        public static string 属性语法糖 { get; set; } = ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");

        /// <summary>
        /// 标准属性
        /// </summary>
        public static string 标准属性
        {
            get
            {
                return ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "");
            }
            set
            {
            }
        }

        /// <summary>
        /// 静态有参方法
        /// </summary>
        /// <param name="isValue"></param>
        /// <returns></returns>
        public static string 静态有参方法(bool isValue)
        {
            return isValue ? $"true:{ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "")}" : $"false:{ConfigHelper.TryGetOtherProject(ProjectsEnum.SurpriseGamePollApi, "ConfigId", "")}";
        }
    }

3.测试代码

代码语言:javascript复制
static void Main(string[] args)
        {
            Environment.SetEnvironmentVariable("DAOKEAPPUK", "jiesan.netcore.surprisegamepollapi");
            ConfigCenterClient.Init(ProjectsEnum.List);

            Console.WriteLine($"------------------------普通 class-----------------");
            Console.WriteLine($"TestStaticService.GetConfigId1={TestStaticService.GetConfigId1}");
            Console.WriteLine($"TestStaticService.GetConfigId2()={TestStaticService.GetConfigId2()}");
            Console.WriteLine($"TestStaticService.GetConfigId3()={TestStaticService.GetConfigId3()}");
            Console.WriteLine($"TestStaticService.GetConfigId4={TestStaticService.GetConfigId4}");
            Console.WriteLine($"TestStaticService.GetConfigId5={TestStaticService.GetConfigId5}");
            Console.WriteLine($"TestStaticService.GetInstance(true)={TestStaticService.GetInstance(true)}");
            Console.WriteLine($"TestStaticService.GetInstance(false)={TestStaticService.GetInstance(false)}");
            Console.WriteLine($"------------------------static class-----------------");
            Console.WriteLine($"TestStaticService2.GetConfigId1={TestStaticService2.GetConfigId1}");
            Console.WriteLine($"TestStaticService2.GetConfigId2()={TestStaticService2.GetConfigId2()}");
            Console.WriteLine($"TestStaticService2.GetConfigId3()={TestStaticService2.GetConfigId3()}");
            Console.WriteLine($"TestStaticService2.GetConfigId4={TestStaticService2.GetConfigId4}");
            Console.WriteLine($"TestStaticService2.GetConfigId5={TestStaticService2.GetConfigId5}");
            Console.WriteLine($"TestStaticService2.GetInstance(true)={TestStaticService2.GetInstance(true)}");
            Console.WriteLine($"TestStaticService2.GetInstance(false)={TestStaticService2.GetInstance(false)}");

            Console.ReadKey();
        }

输出结果:

111=》222 外部配置从111修改为222后 执行的情况

代码语言:javascript复制
------------------------普通 class-----------------
TestCommonService.静态变量=111
TestCommonService.静态方法()=111
TestCommonService.静态方法语法糖=>()=111
TestCommonService.只读属性=111
TestCommonService.属性=111
TestCommonService.标准属性=111
TestCommonService.静态有参方法(true)=true:111
TestCommonService.静态有参方法(false)=false:111
------------------------静态 class-----------------
TestStaticService.静态变量=111
TestStaticService.静态方法()=111
TestStaticService.静态方法语法糖=>()=111
TestStaticService.只读属性=111
TestStaticService.属性语法糖=111
TestStaticService.标准属性=111
TestStaticService.静态有参方法(true)=true:111
TestStaticService.静态有参方法(false)=false:111
[15:59:17 INF] [tcbase.configcenter.client](6.5.3) jiesan.netcore.surprisegamepollapi配置项已改变,正在重新加载
 ------------------------普通 class-----------------
TestCommonService.静态变量=111
TestCommonService.静态方法()=222
TestCommonService.静态方法语法糖=>()=222
TestCommonService.只读属性=222
TestCommonService.属性=111
TestCommonService.标准属性=222
TestCommonService.静态有参方法(true)=true:222
TestCommonService.静态有参方法(false)=false:222
------------------------静态 class-----------------
TestStaticService.静态变量=111
TestStaticService.静态方法()=222
TestStaticService.静态方法语法糖=>()=222
TestStaticService.只读属性=222
TestStaticService.属性语法糖=111
TestStaticService.标准属性=222
TestStaticService.静态有参方法(true)=true:222
TestStaticService.静态有参方法(false)=false:222

版权属于:dingzhenhua

本文链接:https://cloud.tencent.com/developer/article/2019131

转载时须注明出处及本声明

set

0 人点赞