首頁  >  文章  >  後端開發  >  在 PHP 中重寫

在 PHP 中重寫

WBOY
WBOY原創
2024-08-29 12:59:101029瀏覽

重寫是一個物件導向程式設計概念,類似 PHP 中的類別、物件、封裝、多型、重載等概念。當在衍生類別中建立與基底類別或父類別中的方法相同的方法時,就會完成函數和類別的重寫。這兩個方法具有相同的名稱和相同數量的參數。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

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

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

覆蓋如何運作?

讓我們來探索一下 PHP 中的重寫是如何運作的。

  • 重寫是一個概念,其中基底類別的衍生類別執行與基底類別相同的操作。這種重寫可以在方法或類別上。如果是方法重寫,那麼方法的名稱、參數、存取說明符會發現與父類別方法的存取說明符相同。如果從基底類別派生的子類別中沒有發現該方法的實作發生變化,則稱該子類別的方法已經覆寫了基底類別的方法。
  • 假設有一個名為 Person 的類,它有自己的資料屬性和方法。這是具有名為 talk() 的方法的父類別或基底類別 Person。現在又多了一個名為 Employee 的類,它擴充了 Person 類別。現在,這個 Employee 類別擁有自己的資料屬性和方法,並且還具有與父類別相同的方法,例如.speak()。
  • 這個speaks()方法存在於基底類別中,也存在於衍生類別中。
  • 現在發生的是當類別實例化或建立物件時,將執行speak()方法中的哪一個,這表示執行speak 基底類別或speak 衍生類別取決於物件類別的名稱稱為。
  • 換句話說,如果建立了 Person 類別的對象,則呼叫並執行 Person 類別的 talk() 方法。但如果建立了 Employee 類別的對象,則會執行衍生類別的 talk() 方法,該方法會覆寫父類別的 talk() 類別。
  • 下面是如何透過範例進行覆蓋
  • 在下面的範例中,有兩個類,基底類和衍生類。衍生類別擴展了基底類別。這些類別被啟動並創建兩個物件 $obj1 和 $obj2。 $obj1 是 BaseClass 的對象,$obj2 是衍生類別的對象。這些物件進一步呼叫各自類別中聲明的方法。
  • 在這裡您將觀察到基底類別和衍生類別具有相同的方法,稱為 ABC()。當你執行這個程式時,你會注意到 ABC() 方法已經覆寫了基底類別方法 ABC()。

方法重寫範例

方法重寫的例子如下。

代碼:

class BaseClass {
public function ABC() {
echo "<br /> In the base class";
}
}
class DerivedClass extends BaseClass {
// override the method ABC() of base class
public function ABC() {
echo "<br />In the derived class";
}
}
$obj1 = new BaseClass;
$obj1->ABC();
$obj2 = new DerivedClass;
$obj2->ABC();

輸出:

在 PHP 中重寫

使用存取修飾符覆蓋

共有三個存取修飾符。

  1. 公用:可從程式中的任何位置存取。
  2. 私有:只能從父類別存取。
  3. 受保護:可從基底類別和衍生類別存取。

眾所周知,受保護的方法可以從基底類別和衍生類別訪問,它可以在子類別中公開,但不能私有,因為私有方法只能在父類別中存取。此外,如果類別方法的存取說明符為 public,則衍生類別中的重寫方法不能宣告為 private 和 protected

使用存取修飾符覆蓋方法的範例

下面寫了一個使用存取修飾符進行覆蓋的範例。

代碼:

class BaseClass {
private function ABC() {
echo "<br/>In the base class Method : ABC";
}
protected function XYZ() {
echo "<br/>In the base class Method : XYZ";
}
}
class DerivedClass extends BaseClass {
// overriding with public for wider accessibility
public function ABC() {
echo "<br/> In the derived class Method : ABC";
}
// overriding method
// with more accessibility
public function XYZ() {
echo "<br/>In the derived class Method : XYZ";
}
}
//$obj1 = new BaseClass;
//$obj1->ABC();   //throws fatal error
//$obj1->XYZ();          //throws fatal error
$obj2 = new DerivedClass;
$obj2->ABC();
$obj2->XYZ();

輸出:

在 PHP 中重寫

使用 Final 關鍵字覆蓋

final 關鍵字用於類別和方法。可以覆蓋方法和類,而不是變數。

最終方法重寫

當一個方法或類別被宣告為final時,則無法對該方法或類別進行重寫,且該類別的繼承也是不可能的。

使用 Final 關鍵字覆寫方法的範例

使用final關鍵字覆蓋的例子如下。

代碼:

class BaseClass {
// Final method – display
// this cannot be overridden in base class
final function display() {
echo "<br /> In the Base class display function";
}
/// method - ABC
function ABC() {
echo "<br /> In the Base cLass ABC function";
}
}
class DerivedClass extends BaseClass {
function ABC() {
echo "<br /> In the Derived class ABC function";
}
}
$obj1 = new DerivedClass;
$obj1->display();
$obj1->ABC();

輸出:

在 PHP 中重寫

Final Class Overriding

A class declared as final cannot be inherited. A Final Class further have final method along with other methods. But since the class itself is declared final there is no use of declaring a final method in a final class.

Example of Class Overriding using Final Keyword

The example of class overriding using final keyword is written below.

Code:

// class declared as final cannot be overridden
final class BaseClass {
// method - ABC
function ABC() {
echo "<br> In the BaseClass Method ABC function";
}
// Final method - display
function display() {
echo "<br> In the BaseClass Method display function";
}
}
// here you cannot extend the base class
// as the base class is declared as final
$obj1 = new BaseClass;
$obj1->display();
$obj1->ABC();

Output:

在 PHP 中重寫

以上是在 PHP 中重寫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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