Java 使用运行时调用的抽象方法的值
我正在尝试使用来自的MyLocation类。在下面的代码中,我需要在实例化MyLocation的类中的任意位置访问变量currentLat和currentLon。我不知道如何访问Java 使用运行时调用的抽象方法的值,java,android,abstract-class,android-location,abstract-methods,Java,Android,Abstract Class,Android Location,Abstract Methods,我正在尝试使用来自的MyLocation类。在下面的代码中,我需要在实例化MyLocation的类中的任意位置访问变量currentLat和currentLon。我不知道如何访问currentLat和currentLon LocationResult LocationResult=新的LocationResult(){ @凌驾 公共位置(位置){ currentLat=location.getLatitude(); currentLon=location.getLongitude(); };
currentLat
和currentLon
LocationResult LocationResult=新的LocationResult(){
@凌驾
公共位置(位置){
currentLat=location.getLatitude();
currentLon=location.getLongitude();
};
}
MyLocation MyLocation=新的MyLocation();
myLocation.getLocation(这是locationResult)代码>
假设我想要这里
Double x=currentLoc代码>
我该怎么做?如果您有任何帮助,请使用您自己的扩展/实现LocationResult类/接口并像这样添加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();
您可以对变量使用静态
修饰符并全局定义它们
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;
它返回0.0我也尝试了setter,但没有帮助。在显示MYLocationResult值的gotLocation方法内部,但在任何地方都是0.0。只有在之前调用gotLocation()时,才能调用getCurrentLat,因此,如果您需要getCurrentLat,但尚未设置它,您将获得0.0我获得0.0,但内部位置结果值不是0您确定该值不是0.0
?正在尝试将值记录在此行之后currentLat=location.getLatitude()代码>放这行,然后再试<代码>Log.e(“method1返回”,“位置+location.getLatitude())代码>