When using map functions in java I can do the following:
在java中使用map函数时,我可以执行以下操作:
import com.example.MyClass;
someStream.map(MyClass::myStaticMethod)
but in my project we sometimes use static imports, how can I reference the myStaticMethod when the import is static?
但是在我的项目中我们有时会使用静态导入,当导入是静态的时候如何引用myStaticMethod呢?
I would think this would work but it doesn't:
我认为这会起作用,但它不会:
import static com.example.MyClass.myStaticMethod;
someStream.map(myStaticMethod); //does not compile
Why does this not work? Am I 'stuck' with using the first example or are there other solutions.
为什么这不起作用?我是否仍然坚持使用第一个例子或者有其他解决方案。
4 个解决方案
#1
29
Let's look at the relevant part of the Java Language Specification, 15.13. Method Reference Expressions.
让我们看看Java语言规范的相关部分,15.13。方法参考表达式。
It lists the following ways to a create method reference:
它列出了以下创建方法引用的方法:
MethodReference:
ExpressionName :: [TypeArguments] Identifier
ReferenceType :: [TypeArguments] Identifier
Primary :: [TypeArguments] Identifier
super :: [TypeArguments] Identifier
TypeName . super :: [TypeArguments] Identifier
ClassType :: [TypeArguments] new
ArrayType :: new
Note that all of them include a ::
token.
请注意,所有这些都包含:: token。
Since the argument to someStream.map(myStaticMethod)
does not include ::
, it is not a valid method reference.
由于someStream.map(myStaticMethod)的参数不包含::,因此它不是有效的方法引用。
This suggests that you do need to import MyClass
(perhaps in addition to the static import, if that's your preference) and refer to the method as MyClass::myStaticMethod
.
这表明您确实需要导入MyClass(可能除了静态导入之外,如果这是您的偏好)并将该方法称为MyClass :: myStaticMethod。
#2
4
Well it does not compile because the JLS says so. But that can't even compile since it would be a plain method invocation and not a method reference - which is defined ONLY using the ::
notation, be it a static or instance used on.
好吧它没有编译,因为JLS这么说。但是这甚至无法编译,因为它将是一个简单的方法调用而不是方法引用 - 它仅使用:: notation定义,无论是使用静态还是实例。
It is an interesting aspect still, one that you can't resolve - may be at the moment. There is no language support to define something like:
这是一个有趣的方面,你无法解决的问题 - 可能就在此刻。没有语言支持来定义类似的东西:
MethodRef <Class, Method> ref = ...
And later use that the way you want it to. But I don't even think that would be possible, because you would also have to define the parameters types and return type somehow, because this would be needed to see if it matches multiple other places. Like Predicate<String>
and Function<String, Boolean>
that would potentially apply to the same method reference
.
然后以你想要的方式使用它。但我甚至认为这是不可能的,因为您还必须以某种方式定义参数类型和返回类型,因为这需要查看它是否与其他多个位置匹配。像Predicate
#3
3
Having a static import of a method does not influence the way you define a method reference to it.
静态导入方法不会影响您定义方法引用的方式。
So, if you want it to work, it should look exactly the same just like before introducing the static import:
因此,如果您希望它工作,它应该看起来与引入静态导入之前完全相同:
MyClass::myStaticMethod
#4
-2
With a static import of a method you can only call but not refer to it. For stream().map() it needs a reference to a function so it can call it for every entry.
使用方法的静态导入,您只能调用但不能引用它。对于stream()。map(),它需要对函数的引用,以便它可以为每个条目调用它。
#1
29
Let's look at the relevant part of the Java Language Specification, 15.13. Method Reference Expressions.
让我们看看Java语言规范的相关部分,15.13。方法参考表达式。
It lists the following ways to a create method reference:
它列出了以下创建方法引用的方法:
MethodReference:
ExpressionName :: [TypeArguments] Identifier
ReferenceType :: [TypeArguments] Identifier
Primary :: [TypeArguments] Identifier
super :: [TypeArguments] Identifier
TypeName . super :: [TypeArguments] Identifier
ClassType :: [TypeArguments] new
ArrayType :: new
Note that all of them include a ::
token.
请注意,所有这些都包含:: token。
Since the argument to someStream.map(myStaticMethod)
does not include ::
, it is not a valid method reference.
由于someStream.map(myStaticMethod)的参数不包含::,因此它不是有效的方法引用。
This suggests that you do need to import MyClass
(perhaps in addition to the static import, if that's your preference) and refer to the method as MyClass::myStaticMethod
.
这表明您确实需要导入MyClass(可能除了静态导入之外,如果这是您的偏好)并将该方法称为MyClass :: myStaticMethod。
#2
4
Well it does not compile because the JLS says so. But that can't even compile since it would be a plain method invocation and not a method reference - which is defined ONLY using the ::
notation, be it a static or instance used on.
好吧它没有编译,因为JLS这么说。但是这甚至无法编译,因为它将是一个简单的方法调用而不是方法引用 - 它仅使用:: notation定义,无论是使用静态还是实例。
It is an interesting aspect still, one that you can't resolve - may be at the moment. There is no language support to define something like:
这是一个有趣的方面,你无法解决的问题 - 可能就在此刻。没有语言支持来定义类似的东西:
MethodRef <Class, Method> ref = ...
And later use that the way you want it to. But I don't even think that would be possible, because you would also have to define the parameters types and return type somehow, because this would be needed to see if it matches multiple other places. Like Predicate<String>
and Function<String, Boolean>
that would potentially apply to the same method reference
.
然后以你想要的方式使用它。但我甚至认为这是不可能的,因为您还必须以某种方式定义参数类型和返回类型,因为这需要查看它是否与其他多个位置匹配。像Predicate
#3
3
Having a static import of a method does not influence the way you define a method reference to it.
静态导入方法不会影响您定义方法引用的方式。
So, if you want it to work, it should look exactly the same just like before introducing the static import:
因此,如果您希望它工作,它应该看起来与引入静态导入之前完全相同:
MyClass::myStaticMethod
#4
-2
With a static import of a method you can only call but not refer to it. For stream().map() it needs a reference to a function so it can call it for every entry.
使用方法的静态导入,您只能调用但不能引用它。对于stream()。map(),它需要对函数的引用,以便它可以为每个条目调用它。