gpt4 book ai didi

java - Android/Java - 从列表中删除方法引用

转载 作者:行者123 更新时间:2023-12-05 07:43:00 24 4
gpt4 key购买 nike

我有一个方法引用数组列表 (object::method),当用户单击按钮时,我会在其中插入一些方法。问题是我还需要在某些时候删除一些特定的方法引用。

如果我使用 list.remove(object::method) 方法不会被删除,因为 lambda object::method 每次使用它时都有另一个 ID。我希望每个对象::方法都是可移动的,所以我需要一种方法来为每个对象-方法对获取相同的 lambda ID。

有没有办法完成此操作,还是我必须手动跟踪我添加的每个 ID,以便删除它?

最佳答案

来自这个答案https://stackoverflow.com/a/29082592/2657100

the target type of a method reference or a lambda expression should be a functional interface. Based on that only, runtime will create an instance of a class providing implementation of the given functional interface.

尝试在列表中添加相同的方法引用两次。您会看到您将拥有两个不同的对象,并且对彼此调用 equals 可能会返回 false

因此,如果没有 ID 或反射性 Method 实例等辅助结构,您将很难管理功能接口(interface)的实例。

关于java - Android/Java - 从列表中删除方法引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43989973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com