使用运行时调用的抽象方法的值
Using value of abstract method called at runtime
我正在尝试使用 here 中的 MyLocation class。在下面的代码中,我需要在实例化 MyLocation 的 class 中的任何位置访问变量 currentLat 和 currentLon。我不知道如何访问 currentLat
和 currentLon
的值
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
currentLat = location.getLatitude();
currentLon = location.getLongitude();
};
}
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
假设我想在这里
Double x =currentLoc;
如何获取?任何帮助将不胜感激
而不是匿名 class 使用你自己的 extends/implments LocationResult class/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();
您可以为您的变量使用 static
修饰符并全局定义它们..
public static double currentLat; // defined globally...
public static double currentLon;
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
currentLat =location.getLatitude(); // assuming getLatitude() returns double or int
currentLon = location.getLongitude();
};
}
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
现在您可以在任何地方访问它们
double x =currentLon;
double y =currentLat;
我正在尝试使用 here 中的 MyLocation class。在下面的代码中,我需要在实例化 MyLocation 的 class 中的任何位置访问变量 currentLat 和 currentLon。我不知道如何访问 currentLat
和 currentLon
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
currentLat = location.getLatitude();
currentLon = location.getLongitude();
};
}
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
假设我想在这里
Double x =currentLoc;
如何获取?任何帮助将不胜感激
而不是匿名 class 使用你自己的 extends/implments LocationResult class/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();
您可以为您的变量使用 static
修饰符并全局定义它们..
public static double currentLat; // defined globally...
public static double currentLon;
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
currentLat =location.getLatitude(); // assuming getLatitude() returns double or int
currentLon = location.getLongitude();
};
}
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
现在您可以在任何地方访问它们
double x =currentLon;
double y =currentLat;