kotlin 的 Application 单例模式

class NewsApplication : Application() {
private var sqlHelper: SQLHelper? = null

    /** 获取数据库Helper  */
    fun getSQLHelper(): SQLHelper {
        if (sqlHelper == null) {
            sqlHelper = SQLHelper(instance)
        }
        return sqlHelper as SQLHelper
    }

    companion object {
        var isIsLogin = false
        //获取单例
        @JvmStatic lateinit  var instance: SportsApplication
            private set
    }
}


//调用
SportsApplication.getInstance()
SportsApplication.getInstance().getSQLHelper()

你可能感兴趣的:(kotlin 的 Application 单例模式)