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 Java 8. It gives a succinct and rich method for alluding to Java 8. It gives a succinct and rich method for alluding to 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 invesgate the language abouttigate the language of adronle, ministraet, formgate the language admingate, ministra uny拍, ministraet, formgate the lanc 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 nceinstance v.
Example// 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
方法2:使用實例變數使用雙冒號運算子進行方法參考
Example
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
方法3: 使用雙冒號運算子引用任意物件的實例方法的方法。
Example
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
Approach 4: Constructor referencing using double colon operator.
Example
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]
結論
以上是Java中的雙冒號(::)運算符的詳細內容。更多資訊請關注PHP中文網其他相關文章!