举个例子:
在同一个包下,有两个class A和B,都实现class C下一个接口,重写了里面的onImageLoaded()这个方法,代码如下:
public interface OnImageLoadListener {
public void onImageLoaded(int id, Uri imageUri, Drawable image);
}
然后我们在C下回调onImageLoaded()方法,代码如下:
private void notifyListeners(LoadResult result) {
for (final OnImageLoadListener listener : mImageLoadListeners) {
listener.onImageLoaded(result.id, result.image, result.drawable);
}
}
我们在A和B里的重写方法onImageLoaded()中打log,当我们触发B中的点击事件时,两个重写方法下都会打印出log,问题是这两个重写方法 为什么都会被调用?
------解决思路----------------------
不太清楚这个业务是什么,
如果只是想打印A或B的onImageLoaded
那么可以把C写成一个抽象类(或者写个D继承C),总之提供打印方法notifyListeners
private void notifyListeners(LoadResult result) {
getListener().onImageLoaded(result.id, result.image, result.drawable);
}
getListener()自然是获取实例,就可以获得具体的子类了。
然后在子类里重写onImageLoaded方法,这样子类调用notifyListeners就会只打印关于该子类的log