国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php實現parent調用父類的構造方法與被覆寫的方法

2024-05-04 21:54:46
字體:
來源:轉載
供稿:網友

 這篇文章主要介紹了php實現parent調用父類的構造方法與被覆寫的方法,在上一篇關于使用類繼承解決代碼重復問題的基礎上,進一步分析了parent調用父類的構造方法與被覆寫的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

  

本文實例講述了php實現parent調用父類的構造方法與被覆寫的方法。分享給大家供大家參考。具體分析如下:

覆寫:被重新設計。

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

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

parent 調用父類的構造方法

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

所以, parent::__construct() 以為著調用父類的 __construct() 方法。

修改上篇《使用類繼承解決代碼重復等問題》中的代碼,讓每個類只處理自己的數據:

復制代碼 代碼如下:<?php
header('Content-type:text/html;charset=utf-8');
// 從這篇開始,類名首字母一律大寫,規范寫法
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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舒城县| 扎鲁特旗| 邯郸市| 防城港市| 泗阳县| 安乡县| 友谊县| 射阳县| 柘城县| 峨山| 图木舒克市| 泾阳县| 湄潭县| 吉木乃县| 溆浦县| 定远县| 丁青县| 浠水县| 泰宁县| 疏勒县| 甘德县| 沽源县| 娄烦县| 安宁市| 原平市| 崇明县| 曲沃县| 甘孜| 平泉县| 共和县| 东兰县| 翁源县| 平湖市| 灵丘县| 安阳市| 炎陵县| 化州市| 错那县| 互助| 合水县| 河北省|