首頁  >  文章  >  Java  >  Java 中的物件導向程式設計

Java 中的物件導向程式設計

WBOY
WBOY原創
2024-08-30 16:20:181080瀏覽

Java 是 James Gosling 設計的物件導向程式設計。它是一種基於類別並具有並發程式設計功能的通用程式語言。它也具有多線程功能。它是一種靜態、安全、強型別的程式語言。它由 Oracle 公司(當時的 Sun 公司)開發和維護。其檔案副檔名是.java或.class。它首次出現於 1995 年。它的目的是開發可以編寫一次、隨處運行的應用程式。它在客戶端-伺服器類型的應用程式中最受歡迎。它根據 GNU 通用公共許可證和 Java 社區進程獲得許可。 Java 的最新版本是 10,於 2018 年 3 月發布。

Java 物件導向程式設計

Java 程式語言是基於物件導向的程式設計方法或範式,具有不同類型的概念,例如類別、物件、繼承、多態性、封裝和抽象,可描述如下:

廣告 該類別中的熱門課程 程式語言 - 專業化 | 54 課程系列 | 4 次模擬測驗
  • 類別:這是物件的藍圖,定義了真正功能所在的欄位或屬性和方法。這些屬性和方法稱為成員,在宣告成員時可以根據定義的存取修飾符來存取這些成員。
  • 物件:物件稱為類別的實例,可以透過呼叫類別的建構子來宣告和實例化。物件將具有狀態,狀態將包含類別的屬性將保存的資料。
  • 繼承:這是第三步驟過程。對資料進行檢查、清洗、轉換和視覺化,減少無用資訊,將其轉化為重要的資訊集,從現有資料中獲取有價值的資訊。
  • 多態性:多態性被定義為以不同可能的方式執行單一任務的過程。在Java中,多態性可以透過方法重載和方法重寫兩種方式來實現。方法重載也稱為編譯時多態性,而方法重寫也稱為運行時多態性。
  • 封裝:這是封裝,這表示將程式碼隱藏或綁定或包裝到Java中定義為類別的單一單元或模組中。物件導向程式設計的封裝特性可以透過Java中的類別來實現。一個普通的舊 java 物件或 Java Bean 被認為是被封裝的,因為類別的成員是私有的(存取修飾符),這些成員只能透過使用類別中的 getter 和 setters 方法來存取。
  • 抽象:物件導向的功能抽象可以定義為透過僅公開所需的介面或存取方法來呼叫實現類別方法來隱藏功能實現的過程。在Java程式語言中可以透過使用Interface和Abstract Class來實現抽象。

Java 中物件導向程式設計的優點

  1. 它有助於輕鬆開發不同類型的應用程式及其維護,而無需額外成本。
  2. 它有助於透過對設計進行小的更改來輕鬆實現更改,從而使應用程式更能適應客戶所需的較大更改。
  3. 程式碼中的模組化有助於透過輕鬆修復錯誤來輕鬆進行故障排除和維護。
  4. 程式碼復用為主。
  5. 它為頻繁的功能變更提供了更大的靈活性。

 Java 中物件導向程式設計的應用

Java 中物件導向程式設計有不同的應用,以下是該概念領域的範例:

1.班級

類別可以定義如下:

public class Employee {
private int employeeId;
private String employeeName;
public int getSalary(int basicPay, int da, int hra) {
int salary = basicPay + da + hra;
return salary;
}
}

在上面的類別中,employeeId、employee name 和 getSalary() 方法是類別的成員,而 employeeId 和 employee name 是屬性或字段,getSalary() 是完成實際工作的方法。

2.對象

可以為上述 Employee 類別建立一個對象,如下所示。

Employee employeeObject = new Employee();

在上面的行中,使用 new 關鍵字建立了一個對象,而 Employee() 是用來建立該物件的空參數建構子。員工反對對 Employee 類別的引用。

3.多態性

這可以透過方法重寫和重載來實現。

public int getSalary(int basicPay, int da, int hra) {
int salary = basicPay + da + hra;
return salary;
}

在上面的方法中,可以透過加入括號中來將另一個參數加入方法 getSalary() 中,如下所示:

public int getSalary(int basicPay, int da, int hra, int bonus) {
int salary = basicPay + da + hra + bonus;
return salary;
}

4. Encapsulation

This can be achieved as below:

public class Employee {
private int employeeId;
private String employeeName;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
}

The above class Employee has two fields (private) and four methods (getters and setters) which will be used to access the above two private attributes.

5. Abstraction

This is the process of hiding the implementation functionality.

In the above method getSalary(), the internal function of the addition of all the components of a salary is hidden inside the method, and only this can be accessed by using the method name by passing the values as method arguments. In this way, the total salary will be obtained by passing the individual salary components to the method.

Conclusion

There are different and multiples areas of applications in the field of the Web world, Standalone, and many other areas for the Object-Oriented Programming in Java concept. The average utilization or application of object-oriented programming in Java has been in the top 5 positions for most of the enterprise applications and has been in almost every enterprise as of now is the most sought-after technology. There are huge numbers of tools available, such as IDEs, to develop applications using object-oriented programming in Java. Many companies are using Java-based applications for their requirements because of the ease of development and maintenance. The standalone apps developed in Java are mostly being used by many companies for their in-house tools They are developed based on Java Swing GUI toolkit and are now called Java FX in its recent version. The recent version of Java 8 provides great functional programming features and parallel processing capabilities with its Stream API.

Recommended Articles:

This has been a guide to Object-Oriented Programming in Java. Here we have discussed the Different concepts and the applications of Object-Oriented Programming in Java. You may also look at the following article to learn more –

  1. Oops, Java Interview Questions
  2. Java Testing Interview Questions
  3. Object in Java
  4. Overloading and Overriding in C#

以上是Java 中的物件導向程式設計的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn