一种在类中传递资源的方式

背景:

直接传递,参数过多,所以不直接传递

成员变量,资源的释放成为问题,造成内存泄漏

 

解决:

//private List resource;

private WeakReference<List> resourc;

 

func() {

//resource = xxx;

List tempList = xxx;

resource = new WeakReference<List>(tempList)

 

无论如何,tempList出了func作用域随即解绑,xxx的返回只剩一个弱引用成员变量resource

posted on 2025-07-08 19:20  silyvin  阅读(23)  评论(0)    收藏  举报