java - 使用运行时调用的抽象方法的值

标签 java android abstract-class android-location abstract-methods

我正在尝试使用 here 中的 MyLocation 类。在下面的代码中,我需要在实例化 MyLocation 的类中的任何位置访问变量 currentLat 和 currentLon。我不知道如何访问 currentLatcurrentLon

的值

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/

相关文章:

java - 在 Spring 应用程序上下文文件中定义列表

Java 代理套接字未连接到客户端

java - 如何确定图数据结构中某个节点是否有多个输入?

java - 替换android中的字符串?

java-反射: How to Override private static abstract inner class method?

java - 抽象类错误。原因: actual and formal arguments differ in length

java - Websockets、SockJs、Stomp、Spring、RabbitMQ、自动删除用户特定队列

android - 使用单个 TableLayout 而不是多个 LinearLayouts 的优势,因为 TableRow 仅扩展 LinearLayout

android - 在 linux 下从 jenkins 构建 android 项目 - 构建失败,找不到导入的 build.xml

java - 无法将抽象类转换为实际对象