无法理解LocationFinder.getFinder参数列表中代码的工作情况,即类新的LocationFinder.Listener()
让我明白那是什么。
private LocationFinder locationFinder;
private ViewMaster viewMaster;
private synchronized void initLocationFinder() {
if (locationFinder == null) {
**locationFinder =LocationFinder.getFinder(new LocationFinder.Listener()
{
public void newLocation(double lat, double lon, int accuracy) {
DataModel.getInstance().setCurrentPosition(new GeoCoordinate(lat, lon, 0), accuracy);
refreshCurrentPositionOnMap();
if (viewMaster != null) {
viewMaster.draw();
}
}
});**
}
}
其中LocationFinder是一个抽象类
public static LocationFinder getFinder(Listener listener)
{
// returns finder which is reference of LocationFinder class
}
和Listener是一个接口
public interface Listener {
void newLocation(double lat, double lon, int accuracy);
}
但ViewMaster是最终类扩展GameCanvas
public final class ViewMaster extends GameCanvas {
private volatile boolean refreshScreen = false;
public final void draw() {
refreshScreen = true;
}
这里的volatile布尔值是什么意思??
答案 0 :(得分:0)
1)您的LocationFinder getFinder(Listener listener)
使用Listener
作为参数。您显示的代码是创建接口的匿名实例,并为类体中的接口方法void newLocation(double lat, double lon, int accuracy);
提供实现。
2)当您拥有共享变量时使用volatile
,并且这些变量由不同的线程访问,并提供一种机制让所有线程看到变量的一致值。请参阅JLS - chapter 8 about volatile fields.