为什么Java 8选择双冒号运算符?

4
在Java 8之前,我观察到指称成员方法的事实标准是使用“#”符号(例如Object#toString())。然后Java 8推出了“::”运算符作为方法引用,而似乎没有明确的原因选择这种方式。官方有解释或理由吗?

在哪里使用 # 来引用成员方法? - Kayaman
Java没有“成员方法”的概念;在Java中,函数不是一等公民。::只是被借用来定义一个方便的方法引用方式,左边部分是它的类或实例,右边部分是方法名。 - fge
@gontard 我也不知道,但我觉得这有点有趣。我本想问“这怎么算是基于观点的?”,但他们可能会因为这个评论主要基于观点而关闭它。 :) - BambooleanLogic
1
@gontard:谢谢,这正是我在寻找的。如果这个问题重新开放,请随意将其作为答案发布(附有摘录或其他内容以遵循无链接指南),我会很乐意接受它。 - BambooleanLogic
显示剩余2条评论
1个回答

7
你可以在Brian Goetz那里找到一个官方的解释或理由。你应该阅读完整的讨论,但这是一个摘录:

The :: infix syntax:

   ClassName::methodName 
   ClassName<T>::methodName 
   ClassName::<U>genericMethodName 

works acceptably well. Some people like it, and some people hate it -- just like #. There's never going to be a perfect syntax for anything that makes everyone jump up in unison and say "yeah, that's it!" But :: is OK, and using up :: here is far better than using up #. (And, while this might look a little weird to C++ programmers, the overlap between the Java and C++ developer bases at this point is small enough that I don't think we should be too worried about that.)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接