当属性声明包含 static 修饰符时,称该属性为静态属性。当不存在 static 修饰符时,称该属性为实例属性。

静态属性不与特定实例相关联,因此在静态属性的访问器内引用 this 是编译时错误。

静态属性和方法都不能在实例化的对象中引用,而且静态属性和静态方法都不能使用this关键字来加以限定,而只能用类名来加以限定。在调用静态方法时直接使用类引用即可。

 

静态属性实例代码:

        public static RepositoryFactory Singleton
        {
            get { return GetInstance(); }
        }

应用:

       private readonly IDictionary<String, Object> factory = new Dictionary<String, Object>();

        #region RepositoryFactory 为单例
        /// <summary>
        /// RepositoryFactory 为单例模式
        /// </summary>
        private static RepositoryFactory Instance;
        private RepositoryFactory()
        {
        }
        /// <summary>
        /// 获取Repository工厂实例
        /// </summary>
        /// <returns>Repository工厂实例</returns>
        public static RepositoryFactory GetInstance()
        {
            if (Instance == null) Instance = new RepositoryFactory();
            return Instance;
        }
        public static RepositoryFactory Singleton
        {
            get { return GetInstance(); }
        }
        #endregion

 

如果在上一层调用: RepositoryFactory.Singleton 就可以。

但是如果 是实例属性:

        public  RepositoryFactory Singleton
        {
            get { return GetInstance(); }
        }
RepositoryFactory.Singleton 这样就不可以了。

必须是 new RepositoryFactory().Singleton 才可以。

相关文章:

  • 2021-05-11
  • 2021-12-08
  • 2022-12-23
  • 2021-07-07
  • 2021-06-10
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
  • 2021-12-28
  • 2021-07-03
  • 2022-12-23
  • 2021-08-18
相关资源
相似解决方案