我有以下课程:
public class MyGLSurfaceView extends GLSurfaceView {
MyRenderer mRenderer;
public MyGLSurfaceView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init();
}
我想添加以下构造函数:
public MyGLSurfaceView(Context context, MyRenderer myRenderer) {
我试过了:
public class MyGLSurfaceView extends GLSurfaceView {
MyRenderer mRenderer;
static AttributeSet attributeSet = null;
public MyGLSurfaceView(Context context, MyRenderer mRenderer) {
this.mRenderer = mRenderer;
this(context, attributeSet);
}
public MyGLSurfaceView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init();
}
但这不会让我在调用默认构造函数之前设置渲染器MyGLSurfaceView
。
如何在调用默认构造函数之前设置变量?
在设置任何字段之前,您必须完成调用任何基本构造函数(无论是在此类中还是在超类中定义的)。因此,如果需要调用mRenderer
before init()
进行设置,那么最好的办法就是将两个构造函数分开(如下所示):
public class MyGLSurfaceView extends GLSurfaceView {
MyRenderer mRenderer;
static AttributeSet attributeSet = null;
public MyGLSurfaceView(Context context, MyRenderer mRenderer) {
super(context, attributeSet);
this.mRenderer = mRenderer;
init();
}
public MyGLSurfaceView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句