匿名类方法参数如何返回到原来的实例化类?

发布于 2024-12-01 17:55:49 字数 330 浏览 0 评论 0原文

这是我的代码:

public void pollLocation()
{
    myLocation.getLocation(this, new LocationResult()
    {
        public void gotLocation(Location location)
        {
            //I want to assign Location object from here...
        }
    });
}

private Location lastLocation; //...to here.

这可能吗?

Here's my code:

public void pollLocation()
{
    myLocation.getLocation(this, new LocationResult()
    {
        public void gotLocation(Location location)
        {
            //I want to assign Location object from here...
        }
    });
}

private Location lastLocation; //...to here.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月无声 2024-12-08 17:55:49

是的。一般来说,您可以只写

lastLocation = location

但也许 LocationResult 类/接口也有一个名为 lastLocation 的字段。在这种情况下,您必须编写

OuterClassName.this.lastLocation = location

But 由于看起来您会执行一些异步轮询,因此在没有同步的情况下执行此操作太危险了。此外,您也不会注意到 LastLocation 何时被设置。所以最好在外部类中使用同步setter。

Yes. Generally you can just write

lastLocation = location

But maybe the LocationResult class/interface also has a field named lastLocation. In this case you have to write

OuterClassName.this.lastLocation = location

But since it looks like you would do some asynchronous polling, it's too dangerous to do this without synchronization. Also you wouldn't notice when the lastLocation gets set. So it's better to use a synchronized setter in the outer class.

千仐 2024-12-08 17:55:49

您可以使用设置器:

public void pollLocation()
{
    myLocation.getLocation(this, new LocationResult()
    {
        public void gotLocation(Location location)
        {
            //I want to assign Location object from here...
            setLastLocation(...);
        }
    });
}

private Location lastLocation; //...to here.
private void setLastLocation(Location l) { lastLocation = l; }

只需小心多线程问题。如果您使用多个线程,最好声明lastLocation volatile 或使用AtomicReference。否则,您的代码可能无法按您的预期工作。

You could use a setter:

public void pollLocation()
{
    myLocation.getLocation(this, new LocationResult()
    {
        public void gotLocation(Location location)
        {
            //I want to assign Location object from here...
            setLastLocation(...);
        }
    });
}

private Location lastLocation; //...to here.
private void setLastLocation(Location l) { lastLocation = l; }

Just be careful about multithreading issues. If you are using multiple threads, you'd better declare lastLocation volatile or use an AtomicReference. Otherwise, your code might not work as you expect.

痞味浪人 2024-12-08 17:55:49

将lastLocation设置为最终的 java.util.concurrent .atomic.AtomicReference 并将其设置为您喜欢的内容。

(编辑:您确定简单的分配不起作用吗?看起来在 Eclipse 中可以起作用......)

Make lastLocation a final java.util.concurrent.atomic.AtomicReference and set it to your heart's content.

(Edit: you sure a simple assignment doesn't work? It looks like that works in Eclipse ...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文