首頁 > Java > java教程 > 主體

Java中的內部類別

WBOY
發布: 2024-08-30 15:56:59
原創
940 人瀏覽過

Java 程式中的內部類別只不過是在已經運行的類別中聲明和使用的類,以使用外部類別的所有函數和成員存取權限。當程式的編碼模式需要更有組織性同時減少程式碼長度時,通常會使用此方法。內部類別分為三種:成員內部類別、匿名內部類別、局部內部類別。在 Java 程式片段中使用內部類別的優點主要是內部類別可以使程式碼更加優化和組織,提高可讀性,並且此類類別的維護成本非常低。

Java 中內部類別的型別

java中的內部類別基本上是那些不是靜態的並且在另一個類別中聲明的類別。所以內部類別是外部類別的成員。 Java中有3種類型的內部類別:

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

  • 成員,內部類別
  • 本地內部類別
  • 匿名內部類別

請注意,內部類別是一種非靜態的巢狀類別。讓我們一一討論java中不同類型的內部類別。

1.會員內部班

在這種情況下,您只需要在其外部類別中聲明一個類別即可。但該類別一定不能在外部類別的方法內部。需要在外部類別的方法之外定義。內部類別必須是非靜態型別。內部類別類型的物件可以存取外部類別的變數。

2. 本地內部類別

在這種情況下,它也只需要在其外部類別中宣告一個類別。但該類別必須在外部類別的方法內部。需要在外部類別的方法內部定義。內部類別必須是非靜態型別。內部類別類型的物件可以存取外部類別的變數。由於此類是在外部類別的方法內聲明的,因此它的存取也受到限制,類似於局部變數。如果你需要呼叫內部類別的函數,你必須在函數內部實例化它。

本地內部類別需要注意的點:

  • 局部變數的說明符不能是 private、public 或 protected。
  • 不能在函數外部呼叫 Local 內部類別。
  • JDK 1.7 版本之前,局部內部類別無法存取非 Final 局部變數。但是,從 JDK 1.8 版本開始,可以存取本地內部類別中的非最終局部變數。

3.匿名內部類別

匿名,顧名思義,就是沒有名字的通話。但這怎麼可能呢?實際上,它是一種內部類,我們同時進行聲明和實例化(即物件創建)。每當您想要重寫類別的方法時,您可能需要使用匿名內部類別。由於匿名內部類別沒有名稱,因此我們無法建立匿名內部類別建構子。另請注意,您只能在定義時存取匿名內部類別。

匿名內部類別可以透過兩種方式建立。

  • 作為特定類型的子類別。
  • 作為特定介面的實現者。

Java 內部類別範例

下面我們將討論一些Java內部類別的程式碼範例:

範例#1:成員,內部類別

在這個範例中,我們將示範一個Member內部類別的範例。在這個簡單的範例中,我們在外部類別中聲明一個類別。在「OuterClass」內部,我們有一個名為「num1」的私有資料成員和一個內部類別「InnerClass」。我們可以透過「InnerClass」類型的物件存取內部類別內部外部呼叫的私有變數「num1」。透過這種方式,我們利用了內部類別的優勢,外部呼叫的私有成員可以透過內部類別的方法存取。

代碼:

class OuterClass{
private int num1=36;
class InnerClass{
void shw(){
System.out.println("Member Inner Class");
}
}
}
public class DemoOfMemberInnerClass{
public static void main(String args[]){
OuterClass oc=new OuterClass();
OuterClass.InnerClass ic=oc.new InnerClass();
ic.shw();
}
}
登入後複製

輸出:

Java中的內部類別

範例#2:匿名內部類別

在這個範例中,我們將示範 Local 內部類別的範例。這裡我們的主要目標是重寫外部類別方法的功能。顧名思義,類別名稱是明確未知的,因此稱為匿名內部類別。這裡,內部類別的聲明和實例化都是在某個時間點完成的,因此被稱為匿名內部類別。

代碼:

abstract class Cat{
abstract void drink();
}
public class AnonymousInnerClassDemo{
public static void main(String args[]){
Cat ct=new Cat(){ //anonymous inner class
void drink(){
System.out.println("kitty loves milk");
}
};
ct.drink();
}
}
登入後複製

輸出:

Java中的內部類別

Example #3: Method Local Inner Class

In this example, we will demonstrate an example of a method local inner class. In this simple example where we declare a class inside the method “display()” of the outer class “OuterClassDemo”. After that, we can access the method by an object of the outer class.

Code:

public class OuterClassDemo{
private int num1=36;//instance variable
void display(){
class LocalInnerClass{ // local inner class inside the method of outer class
void shw(){
System.out.println(num1);
}
}
LocalInnerClass li=new LocalInnerClass();
li.shw();
}
public static void main(String args[]){
OuterClassDemo obj=new OuterClassDemo();
obj.display();
}
}
登入後複製

Output:

Java中的內部類別

Advantages

Here are some of the advantages of the java inner class, which are explained below:

  • The advantage of an inner class is that it can access all of the members that means functions and variables of the outer class.
  • This feature in Java facilitates us to logically organize our codes based on functionality, increasing code readability, and maintenance. It also facilitates the optimization of your code.
  • An inner class can access all of the private members (which means both data and methods) of its outer class.

Conclusion

This concludes our learning of the topic “Inner Class in Java”. An inner class is a very interesting feature in java where you can use a member of the outer class by the object of its inner class. Write yourself the codes mentioned in the above examples in the java compiler and verify the output. Learning of codes will be incomplete if you will not write code by yourself.

以上是Java中的內部類別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!