封裝是Java中四個基本的物件導向程式設計概念之一。這背後的主要想法是向用戶隱藏實作細節。換句話說,封裝是將資料包裝成一個單元,以防止外界存取它。由於資料對其他類別是隱藏的,因此此過程稱為資料隱藏。讓我們以燈泡的工作為例。即使我們使用它,我們也不知道它是否在燈泡後面工作。 但在 Java 封裝的情況下,可以使用修飾符存取資料。讓我們在下一節中探討一下。
Java 中的封裝是如何運作的?
封裝在 Java 中的工作原理是
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
- 將類別中的屬性或變數宣告為私有
例如,我們正在建立一個 Employee 類別。變數需要設定為私有,如下所示。
private String EmpName; private int EmpID; private int EmpSal;
- 在類別中建立公共方法來取得和設定屬性或變數。
以下是Employee類別中不同私有變數的get方法和set方法。
代碼:
public int getEmpSal() { return EmpSal; }public String getEmpName() { return EmpName; } public int getEmpID() { return EmpID; } public void setEmpSal( int EmpSal) { this.EmpSal = EmpSal; } public void setEmpName(String EmpName) { this.EmpName = EmpName; } public void setEmpID( int EmpID) { this.EmpID = EmpID; }
使用這些方法,可以使類別成為只寫或唯讀,即,如果需要,我們可以跳過這些方法。
Java 封裝的優點
以下是封裝的一些優點。
- 應用簡單
- 能夠依需求重複使用或修改程式碼
- 限制資料的可存取性
- 由於程式碼被封裝,單元測試變得容易
Java Bean 類別是完全封裝類別的一個範例,因為類別中的所有資料成員都是私有的。
Java 封裝範例
讓我們來看一個同時包含 getter 和 setter 方法的封裝範例。為此,創建 2 個類別 - 一個具有主要方法,另一個具有獲取和設定方法。
範例#1
Employee.java
代碼:
//Java program for Encapsulation with both read and write public class Employee { //private variables which can be accessed by public methods of the class private String EmpName; private int EmpID; private int EmpSal; // get method to access the private integer variable EmpSal public int getEmpSal() { return EmpSal; } // get method to access the private string variable EmpName public String getEmpName() { return EmpName; } // get method to access the private integer variable EmpID public int getEmpID() { return EmpID; } // set method to access the private integer variable EmpSal public void setEmpSal( int EmpSal) { this.EmpSal = EmpSal; } // set method to access the private string variable EmpName public void setEmpName(String EmpName) { this.EmpName = EmpName; } // set method to access the private integer variable EmpID public void setEmpID( int EmpID) { this.EmpID = EmpID; } }
- EmployeeEncaps.java
代碼:
public class EmployeeEncaps { public static void main(String[] args) { Employee EmpObj= new Employee(); //object of the class Employee //passing the values to the methods using object EmpObj.setEmpName("Anna"); EmpObj.setEmpSal(30000); EmpObj.setEmpID(670311); // Printing values of the variables System.out.println("Employee's Name: " + EmpObj.getEmpName()); System.out.println("Employee's ID: " + EmpObj.getEmpID()); System.out.println("Employee's Salary: " + EmpObj.getEmpSal()); } }
輸出:
上面的程式中封裝了 Employee 類,因為變數是私有的。由於它具有 get 和 set 方法,因此可以讀取和寫入該實作。 EmpName、EmpSal 和 EmpID 等私有變數透過這些方法進行訪問,並透過使用物件呼叫方法來顯示。
現在,讓我們看看封裝如何與唯讀類別一起使用。
範例#2
- Employee.java
代碼:
//Java program for Encapsulation with read permission public class Employee { //private variables which can be accessed by public methods of the class private String EmpName = "Adam"; private int EmpID = 670388; private int EmpSal = 35000; // get method to access the private integer variable EmpSal public int getEmpSal() {return EmpSal; }// get method to access the private string variable EmpName public String getEmpName() { return EmpName; } // get method to access the private integer variable EmpID public int getEmpID() { return EmpID; } }
- EmployeeEncaps.java
代碼:
public class EmployeeEncaps { public static void main(String[] args) { Employee EmpObj= new Employee(); //object of the class Employee // Printing values of the variables System.out.println("Employee's Name: " + EmpObj.getEmpName()); System.out.println("Employee's ID: " + EmpObj.getEmpID()); System.out.println("Employee's Salary: " + EmpObj.getEmpSal()); } }
輸出:
與第一個範例類似,我們也使用私有變數。不同之處在於我們沒有使用 set 方法來設定類別中私有變數的值。相反,我們直接將值賦給變數。
現在,我們可以進入只寫類別。
範例 #3
- Employee.java
代碼:
//Java program for Encapsulation with write permission public class Employee { //private variables which can be accessed by public methods of the class private String EmpName; private int EmpID; private int EmpSal; // set method to access the private integer variable EmpSal public void setEmpSal( int EmpSal) { this.EmpSal = EmpSal; //for sample output System.out.println("Employee's Salary: " + EmpSal); } // set method to access the private string variable EmpName public void setEmpName(String EmpName) { this.EmpName = EmpName; //for sample output System.out.println("Employee's Name: " + EmpName); }// set method to access the private integer variable EmpID public void setEmpID( int EmpID) { this.EmpID = EmpID; //for sample output System.out.println("Employee's ID: " + EmpID); } }
- EmployeeEncaps.java
代碼:
public class EmployeeEncaps { public static void main(String[] args) { Employee EmpObj= new Employee(); //object of the class Employee //passing the values to the methods using object EmpObj.setEmpName("Iza"); EmpObj.setEmpID(670472); EmpObj.setEmpSal(48000); } }
輸出:
在上面的範例中,我們沒有使用 get 方法來實作只寫類別。即,不能在此處變更或檢索變數。由於無法取得值,因此我們在 set 方法中使用 print 來進行範例輸出。
結論
封裝是一個 OOP 概念,其中資料將被包裝,隱藏所有實作細節。可以透過使用私有變數以及get、setting等方法來存取變數來實現。封裝的主要優點包括靈活性、資料隱藏、易於測試和可重複使用性。
以上是Java中的封裝的詳細內容。更多資訊請關注PHP中文網其他相關文章!

新興技術對Java的平台獨立性既有威脅也有增強。 1)雲計算和容器化技術如Docker增強了Java的平台獨立性,但需要優化以適應不同雲環境。 2)WebAssembly通過GraalVM編譯Java代碼,擴展了其平台獨立性,但需與其他語言競爭性能。

不同JVM實現都能提供平台獨立性,但表現略有不同。 1.OracleHotSpot和OpenJDKJVM在平台獨立性上表現相似,但OpenJDK可能需額外配置。 2.IBMJ9JVM在特定操作系統上表現優化。 3.GraalVM支持多語言,需額外配置。 4.AzulZingJVM需特定平台調整。

平台獨立性通過在多種操作系統上運行同一套代碼,降低開發成本和縮短開發時間。具體表現為:1.減少開發時間,只需維護一套代碼;2.降低維護成本,統一測試流程;3.快速迭代和團隊協作,簡化部署過程。

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

要解決Java應用程序中的平台特定問題,可以採取以下步驟:1.使用Java的System類查看系統屬性以了解運行環境。 2.利用File類或java.nio.file包處理文件路徑。 3.根據操作系統條件加載本地庫。 4.使用VisualVM或JProfiler優化跨平台性能。 5.通過Docker容器化確保測試環境與生產環境一致。 6.利用GitHubActions在多個平台上進行自動化測試。這些方法有助於有效地解決Java應用程序中的平台特定問題。

類加載器通過統一的類文件格式、動態加載、雙親委派模型和平台無關的字節碼,確保Java程序在不同平台上的一致性和兼容性,實現平台獨立性。

Java編譯器生成的代碼是平台無關的,但最終執行的代碼是平台特定的。 1.Java源代碼編譯成平台無關的字節碼。 2.JVM將字節碼轉換為特定平台的機器碼,確保跨平台運行但性能可能不同。

多線程在現代編程中重要,因為它能提高程序的響應性和資源利用率,並處理複雜的並發任務。 JVM通過線程映射、調度機制和同步鎖機制,在不同操作系統上確保多線程的一致性和高效性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1
強大的PHP整合開發環境

WebStorm Mac版
好用的JavaScript開發工具