我正在尝试使用 here 中的 MyLocation 类。在下面的代码中,我需要在实例化 MyLocation 的类中的任何位置访问变量 currentLat 和 currentLon。我不知道如何访问 currentLat
和 currentLon
LocationResult locationResult = new LocationResult(){
@覆盖
公共(public)无效得到位置(位置位置){
currentLat = location.getLatitude();
currentLon = location.getLongitude();
};
}
我的位置 myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
假设我想要这里
双 x =currentLoc;
我怎样才能得到它?任何帮助将不胜感激
最佳答案
使用您自己的扩展/实现 LocationResult 类/接口(interface),而不是匿名类,并像这样添加 getter
class MyLocationResult extends/implments LocationResult{
double currentLat;
double currentLon;
@Override
public void gotLocation(Location location){
currentLat = location.getLatitude();
currentLon = location.getLongitude();
};
public double getCurrentLat(){
return currentLat;
}
public double getCurrentLon (){
return currentLon ;
}
}
然后你可以写
MyLocationResult locationResult = new MyLocationResult();
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
每当您需要 currentLat 或 currentLon 时,您都可以编写
locationResult.getCurrentLat();
关于java - 使用运行时调用的抽象方法的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27940487/