首页 新闻 会员 周边

请问 这是c#什么语法 什么意思 请详细讲解

1
[已解决问题] 解决于 2014-12-17 22:38
 public class Singleton<T> : Singleton
    {
        static T instance;

        /// <summary>The singleton instance for the specified type T. Only one instance (at the time) of this object for each type of T.</summary>
        public static T Instance
        {
            get { return instance; }
            set
            {
                instance = value;
                AllSingletons[typeof(T)] = value;
            }
        }
    }
 public class SingletonList<T> : Singleton<IList<T>>
    {
        static SingletonList()
        {
            Singleton<IList<T>>.Instance = new List<T>();
        }

        /// <summary>The singleton instance for the specified type T. 
        /// Only one instance (at the time) of this list for each type of T.</summary>
        public new static IList<T> Instance
        {
            get { return Singleton<IList<T>>.Instance; }
        }
    }

public new static IList<T> Instance
是什么意思 昨天忘记说那不会了
是不是隐藏父类的这个函数,执行自己的
keeppuching的主页 keeppuching | 初学一级 | 园豆:6
提问于:2014-12-15 21:44
< >
分享
最佳答案
0

    所给代码只是说明泛型类和隐藏父类成员的使用。

    Singleton<T>是类Singleton的泛型类形式,泛型一般应用于集合类,是出于类型安全考虑,比如类IList,IDictionary等。这样Singleton只能存储类型为T的数据。

    Instance并不是Singleton<T>的实例对象,仅仅是Singleton<T>类成员(static),可以直接通过Singleton<T>.Instance访问,不能通过实例化访问。

    SingletonList<T>继承了Singleton<IList<T>>,说明T的类型是IList<T>,由继承关系可以知道SingletonList<T>.Instance也是可行的,而你在子类再次写了Instance实例成员,就会覆盖父类的Instance。

     如果是实例成员,可以用Base替换(Base.Instance),但是Base在Static函数和属性中是不能访问的,所以你直接访使用了Singleton<IList<T>>.Instance。

奖励园豆:5
心梦缘 | 菜鸟二级 |园豆:314 | 2014-12-16 18:53
其他回答(3)
0

估计你的问题是<T>,这个叫泛型。

你google一下,应该有相关的知识。

爱编程的大叔 | 园豆:30839 (高人七级) | 2014-12-15 22:00
0

如果基类有这个方法,如果重写时加 new 就是隐藏基类的同名方法,调用时会直接调用当前的方法。

空明流光 | 园豆:106 (初学一级) | 2014-12-16 12:42
0

泛型~

+小马哥++ | 园豆:906 (小虾三级) | 2014-12-16 15:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册