这是来自 android 开发者网站的代码片段。
class Preview extends ViewGroup implements SurfaceHolder.Callback {
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
Preview(Context context) {
super(context);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
...
}
这里SurfaceHolder
是一个interface
,SurfaceView
是一个类。mSurfaceView.getHolder();
返回SurfaceHolder
。我知道如何创建实现接口的匿名类,并且可以执行类似的操作
SurfaceHolder mHolder = new SurfaceHolder{ ... }
将对匿名类的引用分配给变量名。但是上面的代码片段中的接口是如何实现的呢?