说我有以下代码
public class A {
int x;
public boolean is() {return x%2==0;}
public static boolean is (A a) {return !a.is();}
}
Run Code Online (Sandbox Code Playgroud)
而在另一堂课......
List<A> a = ...
a.stream().filter(b->b.isCool());
a.stream().filter(A::is);
//would be equivalent if the static method is(A a) did not exist
Run Code Online (Sandbox Code Playgroud)
问题是如何使用A :: is类型表示法引用实例方法版本?非常感谢
我试图在我的代码中使用Java 8方法引用.有四种类型的方法参考可用.
随着Static method reference和Constructor reference我有没有问题,但Instance Method (Bound receiver)和Instance Method (UnBound receiver)真搞糊涂了.在Bound接收器中,我们使用Object引用变量来调用方法,如:
objectRef::Instance Method
Run Code Online (Sandbox Code Playgroud)
在UnBound接收器中,我们使用类名来调用方法,如:
ClassName::Instance Method.
Run Code Online (Sandbox Code Playgroud)
我有以下问题:
Bound和Unbound接收方法引用有什么区别?Bound在哪里使用Unbound接收器?我们应该在哪里使我还从Java 8语言特性书中找到了解释Bound和Unbound接收器,但仍然与实际概念混淆.