In Java, the twofold colon (::) administrator, otherwise called the strategy reference administrator, is a strong element presented in Java 8. It gives a succinct and rich method for alluding to techniques or constructors without conjuring them. This administrator improves on the code and upgrades code coherence, making it an important instrument for designers. In this article, we will investigate the language structure of the twofold colon administrator, talk about its applications, and give code guides to better comprehension.
双冒号操作符由两个冒号(::)夹在类名或对象引用和方法名之间。它用作Java中引用方法或构造函数的简写符号。
// A functional interface with a single abstract method interface Printer { void print(String message); } // A class that implements the Printer interface class 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.
// A functional interface with a single abstract method interface 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); } }
Result: 8
Calculator是一个具有一个抽象方法calculate()的函数接口。静态的MathUtils函数add()用于相加两个数字。双冒号操作符创建了一个引用MathUtils的add()方法的Calculator实例。我们使用两个数字调用计算器的compute()方法。控制台输出结果。
Approach 2 involves using the double colon operator to reference an instance method of a particular object.
import 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); } }
Alice Bob
在这个例子中,我们有一个Individual类,其中有一个printName()策略,用于打印个体的名字。我们创建了一个Individual项目的列表,并添加了两个示例。使用双冒号运算符,我们在List接口的forEach()方法中引用了Individual类的printName()策略。这会导致printName()方法被调用,打印列表中每个元素的名字到控制台。
Approach 3 involves referencing an instance method of any arbitrary object of a specific type using the double colon operator.
import 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); } }
Number of palindromic words: 2
In 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.
方法4涉及使用双冒号运算符引用构造函数。
import 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<employee> employeeSupplier = Employee::new; Employee employee = employeeSupplier.get(); employee.name = "John Doe"; employee.age = 30; System.out.println(employee); } } </employee>
Employee [name=John Doe, age=30]
在这个模型中,我们有一个具有定义构造函数的Representative类。利用双冒号操作符,我们创建了一个引用Representative构造函数的Provider功能交互点的实例。然后,我们调用employeeSupplier实例上的get()方法来获取另一个Representative对象。我们设置了员工的姓名和年龄,并使用toString()方法将其打印到控制台。
Java中的双冒号(::)操作符是Java 8中引入的一个强大元素。它提供了一种简洁而丰富的方式来引用方法或构造函数,而不需要直接调用它们。通过使用双冒号操作符,我们可以改进代码,提高可读性,并利用Java中函数式编程的好处。了解双冒号操作符的语法和不同的使用方法对于每个Java开发者来说都是必要的。因此,请务必在将来的Java项目中探索和使用这个特性,以提升您的编码体验。
以上是Java中的双冒号(::)运算符的详细内容。更多信息请关注PHP中文网其他相关文章!