syntax双冒号操作符由两个冒号(::)夹在类名或对象引用和方法名之间。它用作java中引用方法或构造函数的简写符号。
// a functional interface with a single abstract methodinterface printer { void print(string message);}// a class that implements the printer interfaceclass consoleprinter { public static void printmessage(string message) { system.out.println(message); }}public class main { public static void main(string[] args) { printer printer = consoleprinter::printmessage; printer.print(hello, world!); }}
语法解释在上面的代码中,我们使用一个名为printer的实用连接点来定义一个具有单个动态方法print()的类。consoleprinter类实现了该连接点,并为printmessage()方法提供了实现。在principal类中,我们使用双冒号运算符创建了一个printer实例,以引用consoleprinter类的printmessage()方法。最后,我们调用打印机实例的print()方法,该方法又调用printmessage()方法。
算法要在java中使用双冒号运算符,请按照以下步骤进行 -
定义一个具有单个抽象方法的函数式接口。
在一个类中实现接口并提供方法的实现。
use the double colon operator to refer to the method or constructor.
使用双冒号操作符创建函数式接口的实例。
在实例上调用该方法,将调用引用的方法或构造函数。
方法一:使用双冒号操作符的方法引用approach 1 involves using the double colon operator to reference a static method of a class. this approach is useful when we want to pass a method reference that does not depend on any instance variables.
example// a functional interface with a single abstract methodinterface calculator { int calculate(int a, int b);}class mathutils { public static int add(int a, int b) { return a + b; }}public class main { public static void main(string[] args) { calculator calculator = mathutils::add; int result = calculator.calculate(5, 3); system.out.println(result: + result); }}
outputresult: 8
explanationcalculator是一个具有一个抽象方法calculate()的函数接口。静态的mathutils函数add()用于相加两个数字。双冒号操作符创建了一个引用mathutils的add()方法的calculator实例。我们使用两个数字调用计算器的compute()方法。控制台输出结果。
方法2:使用实例变量使用双冒号运算符进行方法引用approach 2 involves using the double colon operator to reference an instance method of a particular object.
exampleimport java.util.arraylist;import java.util.list;class person { private string name; public person(string name) { this.name = name; } public void printname() { system.out.println(name); }}public class main { public static void main(string[] args) { list<person> persons = new arraylist<>(); persons.add(new person(alice)); persons.add(new person(bob)); persons.foreach(person::printname); }}
outputalicebob
explanation在这个例子中,我们有一个individual类,其中有一个printname()策略,用于打印个体的名字。我们创建了一个individual项目的列表,并添加了两个示例。使用双冒号运算符,我们在list接口的foreach()方法中引用了individual类的printname()策略。这会导致printname()方法被调用,打印列表中每个元素的名字到控制台。
方法3: 使用双冒号运算符引用任意对象的实例方法的方法。approach 3 involves referencing an instance method of any arbitrary object of a specific type using the double colon operator.
exampleimport java.util.arrays;import java.util.list;class stringutil { public boolean ispalindrome(string s) { string reversed = new stringbuilder(s).reverse().tostring(); return s.equals(reversed); }}public class main { public static void main(string[] args) { list<string> words = arrays.aslist(level, hello, radar, world); stringutil stringutil = new stringutil(); long count = words.stream().filter(stringutil::ispalindrome).count(); system.out.println(number of palindromic words: + count); }}
outputnumber of palindromic words: 2
explanationin this code scrap, a stringutil class tests for palindromes with ispalindrome(). we create a list of words and use a stream to sort palindromic words using the ispalindrome() method recommended by the twofold colon administrator. control center displays palindromic word count.
approach 4: constructor referencing using double colon operator.方法4涉及使用双冒号运算符引用构造函数。
exampleimport java.util.function.supplier;class employee { public string name; public int age; public employee() { // default constructor } public employee(string name, int age) { this.name = name; this.age = age; } public string tostring() { return employee [name= + name + , age= + age + ]; }}public class main { public static void main(string[] args) { supplier employeesupplier = employee::new; employee employee = employeesupplier.get(); employee.name = john doe; employee.age = 30; system.out.println(employee); }}
outputemployee [name=john doe, age=30]
explanation在这个模型中,我们有一个具有定义构造函数的representative类。利用双冒号操作符,我们创建了一个引用representative构造函数的provider功能交互点的实例。然后,我们调用employeesupplier实例上的get()方法来获取另一个representative对象。我们设置了员工的姓名和年龄,并使用tostring()方法将其打印到控制台。
结论java中的双冒号(::)操作符是java 8中引入的一个强大元素。它提供了一种简洁而丰富的方式来引用方法或构造函数,而不需要直接调用它们。通过使用双冒号操作符,我们可以改进代码,提高可读性,并利用java中函数式编程的好处。了解双冒号操作符的语法和不同的使用方法对于每个java开发者来说都是必要的。因此,请务必在将来的java项目中探索和使用这个特性,以提升您的编码体验。
以上就是java中的双冒号(::)运算符的详细内容。
