php實現(xiàn)parent調(diào)用父類的構(gòu)造方法與被覆寫的方法
本文實例講述了php實現(xiàn)parent調(diào)用父類的構(gòu)造方法與被覆寫的方法。分享給大家供大家參考。具體分析如下:
覆寫:被重新設(shè)計。
在子類中定義構(gòu)造方法時,需要傳遞參數(shù)給父類的構(gòu)造方法,否則我們得到的可能是一個構(gòu)造不完整的對象。
要調(diào)用父類的方法,首先要找到一個引用類本身的途徑:句柄(handle),PHP為此提供了parent關(guān)鍵字。
parent 調(diào)用父類的構(gòu)造方法
要引用一個類而不是對象的方法,可以使用 ::(兩個冒號),而不是 ->。
所以, parent::__construct() 以為著調(diào)用父類的 __construct() 方法。
修改上篇《使用類繼承解決代碼重復(fù)等問題》中的代碼,讓每個類只處理自己的數(shù)據(jù):
header('Content-type:text/html;charset=utf-8');
// 從這篇開始,類名首字母一律大寫,規(guī)范寫法
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;
}
}
?>
每個子類都會在設(shè)置自己的屬性前調(diào)用父類的構(gòu)造方法。基類(父類)現(xiàn)在僅知道自己的數(shù)據(jù),而我們也應(yīng)該盡量避免告訴父類任何關(guān)于子類的信息,這是一條經(jīng)驗規(guī)則,大家想想如果某個子類的信息應(yīng)該是”保密“的,結(jié)果父類知道它的信息,其它子類可以繼承,這樣子類的信息就不保密了。
parent 調(diào)用父類被覆寫的方法
parent 關(guān)鍵字可以在任何覆寫父類的方法中使用。覆寫一個父類的方法時,我們并不希望刪除父類的功能,而是拓展它,通過在當(dāng)前對象中調(diào)用父類的方法可以達到這個目的。
看看上面的代碼,可以發(fā)現(xiàn)兩個子類中 getSummaryLine() 方法中重復(fù)了許多代碼,我們應(yīng)該利用 ShopProduct 類中已經(jīng)存在的功能,而不是重復(fù)開發(fā):
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() 方法完成了”核心“功能,接著在子類中簡單的調(diào)用父類的方法,然后增加更多數(shù)據(jù)到摘要字符串,方法的拓展就實現(xiàn)了。
希望本文所述對大家的php程序設(shè)計有所幫助。
- PHP中子類重載父類的方法【parent::方法名】
- PHP父類調(diào)用子類方法的代碼例子
- php面向?qū)ο蟮姆椒ㄖ剌d兩種版本比較
- php面向?qū)ο笕ヂ?(八)重載新的方法
- PHP面向?qū)ο缶幊讨钊肜斫夥椒ㄖ剌d與方法覆蓋(多態(tài))
- PHP面向?qū)ο蟪绦蛟O(shè)計模擬一般面向?qū)ο笳Z言中的方法重載(overload)示例
- PHP面向?qū)ο蟪绦蛟O(shè)計重載(overloading)操作詳解
- PHP面向?qū)ο蟪绦蛟O(shè)計OOP繼承用法入門示例
- PHP面向?qū)ο罄^承用法詳解(優(yōu)化與減少代碼重復(fù))
- PHP面向?qū)ο蟪绦蛟O(shè)計高級特性詳解(接口,繼承,抽象類,析構(gòu),克隆等)
- PHP面向?qū)ο蟪绦蛟O(shè)計子類擴展父類(子類重新載入父類)操作詳解
相關(guān)文章
1億條數(shù)據(jù)如何分表100張到Mysql數(shù)據(jù)庫中(PHP)
這篇文章主要介紹了當(dāng)數(shù)據(jù)量猛增的時候如何把一億條數(shù)據(jù)分表100張到Mysql數(shù)據(jù)庫中,需要的朋友可以參考下2015-07-07PHP中的session永不過期的解決思路及實現(xiàn)方法分享
讓PHP的session永不過期,你可能沒有遇到這么郁悶的問題,但是我遇到過,很郁悶。2011-04-04不用mod_rewrite直接用php實現(xiàn)偽靜態(tài)化頁面代碼
不用mod_rewrite直接用php代碼實現(xiàn)偽靜態(tài)效果,大家看后就會發(fā)現(xiàn)php真的很方便2008-10-10