码迷,mamicode.com
首页 > 其他好文 > 详细

泛型单例

时间:2019-05-27 13:25:25      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:pre   mono   bug   mon   stat   cti   this   HERE   rac   

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class BaseApp<T>:MonoBehaviour where T:class,new(){
    
    protected static T _instance;

    public static T getInstance(){
        return _instance;
    }

    private void Awake(){
        _instance=this as T;
    }
    
    private void OnDestroy(){
        _instance=null;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class App:BaseApp<App>{

    public void sayHello(){
        Debug.Log("Hello");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test:MonoBehaviour {

    private void Start(){
        App.getInstance().sayHello();//output: Hello
    }
}

泛型单例

标签:pre   mono   bug   mon   stat   cti   this   HERE   rac   

原文地址:https://www.cnblogs.com/kingBook/p/10930017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!