简体   繁体   English

Java 8对静态方法与实例方法的引用

[英]Java 8 reference to a static method vs. instance method

say I have the following code 说我有以下代码

public class A {
    int x;
    public boolean is() {return x%2==0;}
    public static boolean is (A a) {return !a.is();}
}

and in another class... 而在另一堂课......

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

the question is how do I refer to the instance method version using the A::is type notation? 问题是如何使用A :: is类型表示法引用实例方法版本? Thanks a lot 非常感谢

In your example, both the static and the non-static method are applicable for the target type of the filter method. 在您的示例中,静态和非静态方法都适用于过滤器方法的目标类型。 In this case, you can't use a method reference, because the ambiguity can not be resolved. 在这种情况下,您不能使用方法引用,因为无法解决歧义。 See §15.13.1 Compile-Time Declaration of a Method Reference for details, in particular the following quote and the examples below: 有关详细信息,请参见§15.13.1 方法参考的编译时声明 ,特别是以下引用和以下示例:

If the first search produces a static method, and no non-static method is applicable [..], then the compile-time declaration is the result of the first search. 如果第一次搜索产生静态方法,并且没有适用的非静态方法[..],则编译时声明是第一次搜索的结果。 Otherwise, if no static method is applicable [..], and the second search produces a non-static method, then the compile-time declaration is the result of the second search. 否则,如果没有静态方法适用[..],并且第二次搜索产生非静态方法,则编译时声明是第二次搜索的结果。 Otherwise, there is no compile-time declaration. 否则,没有编译时声明。

In this case, you can use a lambda expression instead of a method reference: 在这种情况下,您可以使用lambda表达式而不是方法引用:

a.stream().filter(item -> A.is(item));

The above rule regarding the search for static and non-static methods is somewhat special, because it doesn't matter, which method is the better fit. 关于搜索静态和非静态方法的上述规则有点特殊,因为无关紧要,哪种方法更适合。 Even if the static method would take an Object instead of A , it's still ambiguous. 即使静态方法采用Object而不是A ,它仍然是模棱两可的。 For that reason, I recommend as a general guideline: If there are several methods with the same name in a class (including methods inherited from base classes): 出于这个原因,我建议作为一般指导:如果一个类中有多个具有相同名称的方法(包括从基类继承的方法):

  • All methods should have the same access modifiers, 所有方法都应该具有相同的访问修饰符,
  • All methods should have the same final and abstract modifiers, 所有方法都应该具有相同的final和abstract修饰符,
  • And all methods should have the same static modifier 并且所有方法都应该具有相同的静态修饰符

We can not use not static methods or non-global methods by using className::methodName notation. 我们不能通过使用className :: methodName表示法来使用非静态方法或非全局方法。 If you want to use methods of a particular class you have to have an instance of the class. 如果要使用特定类的方法,则必须具有该类的实例。

So if you want to access is() method then you can use : 
A a = new A();
a.is();
OR 
(new A()).is();

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM