[英]Java local static method reference shorthand syntax
Is there a shorthand way to get a method reference to a local static method the same way the this
keyword or class prefix can be dropped when invoking a method? 是否有一种简便的方法来获取对本地静态方法的方法引用,就像在调用方法时可以删除this
关键字或类前缀一样?
The obvious thing would be to use ::myStaticMethod
but that does not seem to compile: 显而易见的是使用::myStaticMethod
但似乎没有编译:
class MyClass {
static void myStaticMethod () {}
static Runnable runner = ::myStaticMethod; // doesn't compile
// requires MyClass prefix despite being in the same class
}
Alas, there is no shortcut. 唉,没有捷径。 According to the JLS (15.13) , the grammar of method references is as follows: 根据JLS(15.13) ,方法引用的语法如下:
MethodReference: ExpressionName :: [TypeArguments] Identifier ReferenceType :: [TypeArguments] Identifier Primary :: [TypeArguments] Identifier super :: [TypeArguments] Identifier TypeName . super :: [TypeArguments] Identifier ClassType :: [TypeArguments] new ArrayType :: new
In all cases, there's something before the ::
. 在所有情况下,在::
之前都有一些东西。
This grammar is also discussed less formally in the Java tutorial on method references . 在语法参考的Java教程中也不太正式地讨论了这种语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.