首頁  >  文章  >  後端開發  >  php如何實作parent呼叫父類別實例詳解

php如何實作parent呼叫父類別實例詳解

伊谢尔伦
伊谢尔伦原創
2017-07-08 10:24:252042瀏覽

本文實例講述了php實作parent呼叫父類別的建構方法與被覆寫的方法。分享給大家供大家參考。具體分析如下:

覆蓋:被重新設計。

在子類別中定義建構方法時,需要傳遞參數給父類別的建構方法,否則我們得到的可能是一個建構不完整的物件

要呼叫父類別的方法,首先要找到一個引用類別本身的途徑:句柄(handle),PHP為此提供了parent關鍵字
 
parent 呼叫父類別的建構方法

要引用一個類別而不是物件的方法,可以使用::(兩個冒號),而不是-> 。

所以, parent::construct() 以為呼叫父類別的 construct() 方法。

修改上篇《使用類別繼承解決程式碼重複等問題》中的程式碼,讓每個類別只處理自己的資料:

程式碼如下:

<?php
header(&#39;Content-type:text/html;charset=utf-8&#39;);
// 从这篇开始,类名首字母一律大写,规范写法
class ShopProduct{    // 声明类
public $title; // 声明
属性
public $producerMainName;
public $producerFirstName;
public $price;
function construct($title,$firstName,$mainName,$price){
$this -> title = $title;    // 给属性 title 赋传进来的值
$this -> producerFirstName= $firstName; 
$this -> producerMainName = $mainName; 
$this -> price= $price; 
}
function getProducer(){    // 声明方法
return "{$this -> producerFirstName }"."{$this -> producerMainName}";
}
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
return $base;
}
}
class CdProduct extends ShopProduct {
public $playLenth;
function construct($title,$firstName,$mainName,$price,$playLenth){
parent::construct($title,$firstName,$mainName,$price);
$this -> playLenth= $playLenth;
}
function getPlayLength(){
return $this -> playLength;
}
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
$base .= ":playing time - {$this->playLength} )";
return $base;
}
}
// 定义类
class BookProduct extends ShopProduct {
public $numPages;
function construct($title,$firstName,$mainName,$price,$numPages){
parent::construct($title,$firstName,$mainName,$price);
$this -> numPages= $numPages;
}
function getNumberOfPages(){
return $this -> numPages;
}
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
$base .= ":page cont - {$this->numPages} )";
return $base;
}
}
 
?>


每個子類別都會在設定自己的屬性前呼叫父類別的建構方法。基類(父類)現在只知道自己的數據,而我們也應該盡量避免告訴父類任何關於子類的信息,這是一條經驗規則,大家想想如果某個子類的信息應該是」保密「的,結果父類別知道它的訊息,其它子類別可以繼承,這樣子類別的資訊就不保密了。

parent 呼叫父類別被覆寫的方法

parent 關鍵字可以在任何覆寫父類別的方法中使用。覆寫一個父類別的方法時,我們並不希望刪除父類別的功能,而是拓展它,透過在目前物件中呼叫父類別的方法可以達到這個目的。
  看看上面的程式碼,可以發現兩個子類別中getSummaryLine() 方法中重複了許多程式碼,我們應該利用ShopProduct 類別中已經存在的功能,而不是重複開發:

#程式碼如下:

// 父类:ShopProduct
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
return $base;
}
// 子类:CdProduct
function getSummaryLine(){
$base = parent::getSummaryLine();
$base .= ":playing time - {$this->playLength} )";
return $base;
}
// 子类:BookProduct
function getSummaryLine(){
$base = parent::getSummaryLine();
$base .= ":page cont - {$this->numPages} )";
return $base;
}

我們在父類別ShopProduct 中為getSummaryLine() 方法完成了」核心「功能,接著在子類別中簡單的呼叫父類別的方法,然後增加更多資料到摘要字串,方法的拓展就實現了。

以上是php如何實作parent呼叫父類別實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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