Java中的匿名物件


Java 中的匿名物件意味著**在沒有任何引用變數的情況下建立物件**。通常,在 Java 中建立物件時,需要為該物件分配一個名稱。但是,Java 中的匿名物件允許您在沒有為該物件分配任何名稱的情況下建立物件。

因此,如果您只想在一個類中建立一個物件,那麼匿名物件將是一個不錯的方法。閱讀本文,您將瞭解什麼是匿名物件以及如何在 Java 中建立和使用匿名物件。

讓我們開始吧!

Java 中的匿名物件

匿名意味著**無名**。匿名物件基本上是一個已建立但沒有名稱的值。

由於它們沒有名稱,因此除了建立它們的點之外,沒有其他方法可以引用它們。因此,它們具有**“表示式作用域”**,這意味著它們是在單個表示式內建立、評估和銷燬的。

但是,當您希望在程式中不建立超過一個物件時,匿名物件被認為是有用的。

以下是 Java 中建立匿名物件的語法。

語法

new Class_name();

示例

new Student();

以下是如何將引數傳遞給建構函式:

new Student(“Jose”);

如果您希望透過匿名物件呼叫方法,程式碼的編寫方式如下:

new Student().display();

這是一種將引數傳遞給呼叫方法的方式:

new Student().display(“Jose”, 15);

這是一個說明上述概念的程式:

public class Addition { // Declaring the instance variables. int a; int b; int c; int d; // Declaring the parameterize constructor and initializing the parameters. Addition(int p, int q) { a = p; b = q; int ab = a + b; System.out.println("Addition of a and b:" +ab); } // Declaring an instance method and initializing parameters. void addition(int x, int y ) { c = x; d = y; int cd = c + d; System.out.println("addition of c and d:" +cd); } public static void main(String[] args) { // Creating an anonymous object and passing the values to the constructor and calling method. new Addition(2,2).addition(1, 5); // We are also allowed to pass the different values with the same anonymous object. // but we should not create another new anonymous object. new Addition(4,10).addition(5, 15); } }

輸出

Addition of a and b: 4
Addition of c and d: 6
Addition of a and b: 14
Addition of c and d: 20

示例

這是另一個程式,我們將建立一個匿名物件,並透過向建構函式和方法傳遞不同的值來計算正方形的面積和周長。

package anonymousObject; public class Calculation { // Declare instance variable. int a; // Declaration of one parameter constructor. Calculation(int p) { a = p; } // Declaration of instance methods. void area() { int area = a * a; System.out.println("Area of square: " +area); } void perimeter(int b) { int peri = 4 * b; System.out.println("Perimeter of square: " +peri); } public static void main(String[] args) { // Create an anonymous object. new Calculation(50).area(); new Calculation(10).perimeter(100); new Calculation(20).area(); new Calculation(30).perimeter(200); } }

輸出

Area of square: 2500
Perimeter of square: 400
Area of square: 400
Perimeter of square: 800

如何在 Java 中建立和使用匿名物件?

以下程式演示瞭如何使用匿名物件呼叫它們的方法:

class Person { void call(String name) { System.out.println("Hello, " + name + "!"); } int sum(int x, int y) { return x + y; } } new Person().call("Jose"); // Hello, Jose! int calculation = new Person().sum(7, 31); System.out.println(calculation); // 38

由於 Java 10,您可以在例項化期間建立和儲存匿名物件,方法是使用 var 關鍵字儲存例項。

在下面的程式中,您可以看到如何建立一個新物件並從 myObj 引用變數引用它:

boolean result = true; var myObj = new Object() { void greetings() { System.out.println("Hello World!"); } boolean success = result; }; System.out.println(myObj.success); // true myObj.greetings(); // Hello World!

根據周圍的環境,Java 10 中的 var 關鍵字將推斷變數的型別。

在 new Object() 主體內部,您還可以定義物件例項將持有的變數和方法。接下來,您可以像普通物件一樣簡單地呼叫和使用例項變數和方法。

總結

透過以上討論,我們希望您現在已經充分了解了 Java 中的匿名物件是什麼以及如何建立和使用它們。此外,您還將瞭解到,透過在 Java 中使用匿名物件,您可以編寫更少的程式碼並在一個類中只建立一個物件。更重要的是,您不希望編寫未使用的程式碼,並且您還可以防止程式碼在所需作用域之外使用。

如果您喜歡閱讀本文並發現它有用,請給我們豎起大拇指,這將激勵我們為您提供更多有用的技術內容。

更新於:2022年8月25日

13K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.