泛型函數,泛型單例類

    //泛型函數
    private T GetData<T>() where T:Object,new()
    {
        T t = new T();
        return t;
    }
    //泛型單例類
    public class Singleton<T> where T:Singleton<T>,new()
    {
        protected static T instance;
        public static T Instance
        {
            get
            {
                if (instance == null)
                    instance = new T();
                return instance;
            }
        }
        public Singleton()
        {
            if(instance!=null)
            {
                Debug.LogException(new System.Exception(
                    typeof(T).Name + " is Singleton type,can't malloc another"));
            }
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章