我有一个类嵌套是这样的:CSurfaceView - CRectangle - CMesh - CContentReader
即CRectangle要在CSurfaceView上用到,CRecangle包含到CMesh,而CMesh包含CContentReader
其中,CContentReader要用到Context(在Activity中就是this值)读取文件。
这个就要麻烦很多了,Context要经过这么多层传到CContentReader中,
请问,Context要保存到类中,是不是在类中声明Context并在构造函数中赋值就可以了?
------解决方案--------------------
你的想法是对的:
public class CContentReader extends CMesh {
private Context context;
public CContentReader(Context context){
this.context=context;
}
}
------解决方案--------------------
CSurfaceView - CRectangle - CMesh - CContentReader
其他类需要Context么?
如果需要统一设置一个Context的构造函数
如果不需要,也可以给CContentReader设置一个Set访问器