1

无法理解 LocationFinder.getFinder 参数列表中代码的工作方式,即类 new 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 boolean 是什么意思?

4

1 回答 1

0

1)您LocationFinder getFinder(Listener listener)使用 aListener作为参数。void newLocation(double lat, double lon, int accuracy);您显示的代码正在创建接口的匿名实例,并为类体中的接口方法提供实现。

2)volatile当你有共享变量并且这些变量被不同的线程访问时使用,并提供一种机制让所有线程看到变量的一致值。请参阅JLS - 第 8 章关于 volatile 字段。

于 2013-07-06T16:47:53.373 回答