您的位置:首页 > 编程语言

代码优化片段--多处使用同一个对象时

2017-08-11 12:25 288 查看
获取一个数据库管理工具的实例:如果多处使用同一个对象,推荐这样获取实例,防止获得的对象为空或者对象不一致

WeatherDB weatherDB = WeatherDB.getInstance(this);
/**
* 获取weatherDB实例
*/
public synchronized static WeatherDB getInstance(Context context){
if (weatherDB==null) {
weatherDB=new WeatherDB(context);
}
return weatherDB;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐