英文:
Why the need of :: operator when we already have . operator in java?
问题
由于我们可以使用.
运算符访问类的静态字段和方法(使用类名),以及使用对象引用访问实例方法或变量,所以为什么需要::
(方法引用运算符)呢?它也可以做同样的事情,只是更简洁。
英文:
Since, using the . operator we can access the static fields and methods of a class(using the class name) as well as instance methods or variables with the object reference, why do we need the :: (method reference operator) when it does the same thing but only less.
答案1
得分: 2
Java中的“.”操作符用于访问组成对象或类的字段和方法。另一方面,“::”操作符用于引用方法或构造函数,而不实际调用它。有时它被称为方法引用操作符。
“::”操作符在与lambda表达式和函数式编程相关时最常用。它使您可以像处理变量一样使用函数或构造函数,将它们视为一等公民实体。它提供了一种清晰的方式来引用可以放置在变量中或作为参数传递的构造函数或方法。
英文:
The "." operator in Java is used to access the fields and methods that make up an object or class. The "::" operator, on the other hand, is used to refer to a method or constructor without actually calling it. It is sometimes referred to as the method reference operator.
The "::" operator is most frequently used in relation to lambda expressions and functional programming. It enables you to use a function or constructor just like a variable, treating them as first-class entities. It offers a clear way to refer to constructors or methods that may be placed in variables or passed around as arguments.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论