好吧 亲分拿来吧,下面这个满意了么 public sealed class Singleton<T> where T : new()//.net4.0以上可用 { private static readonly Lazy<T> lazy = new Lazy<T>(() => Activator.CreateInstance<T>());//new T()); public static T Instance { get { return lazy.Value; } } private Singleton() { } } public class MyList<T> { private IList<IObserver<T>> myList; public IList<IObserver<T>> MyList { get { return myList; } } public MyList() { myList = new List<IObserver<T>>(); } } 下面是用法,完全解耦单例模式与应用类,嘿嘿 IList<IObserver<T>> obsList = Singleton<MyList<T>>.Instance.MyList;
public T getMyObj()
{
return myObj;
}
public void setMyObj(T myObj)
{
this.myObj = myObj;
}
这样算不算用上泛型了。。
貌似这个答案靠谱点你是弄javaee的么?搞javaweb方面的考这方面的也多么??我即将也要面试啦
public sealed class Singleton<T> where T : new()//.net4.0以上可用
{
private static readonly Lazy<T> lazy =
new Lazy<T>(() => Activator.CreateInstance<T>());//new T()); public static T Instance { get { return lazy.Value; } } private Singleton() { }
}
public class MyList<T>
{
private IList<IObserver<T>> myList;
public IList<IObserver<T>> MyList
{
get { return myList; }
}
public MyList()
{
myList = new List<IObserver<T>>();
}
}
下面是用法,完全解耦单例模式与应用类,嘿嘿
IList<IObserver<T>> obsList = Singleton<MyList<T>>.Instance.MyList;
貌似这个答案靠谱点是的,jee面试题