怎么在php中利用parent調(diào)用父類的構(gòu)造方法-創(chuàng)新互聯(lián)

怎么在php中利用parent調(diào)用父類的構(gòu)造方法?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、禹王臺(tái)網(wǎng)絡(luò)推廣、成都小程序開發(fā)、禹王臺(tái)網(wǎng)絡(luò)營(yíng)銷、禹王臺(tái)企業(yè)策劃、禹王臺(tái)品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供禹王臺(tái)建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:muchs.cn

具體分析如下:

覆寫:被重新設(shè)計(jì)。

在子類中定義構(gòu)造方法時(shí),需要傳遞參數(shù)給父類的構(gòu)造方法,否則我們得到的可能是一個(gè)構(gòu)造不完整的對(duì)象。

要調(diào)用父類的方法,首先要找到一個(gè)引用類本身的途徑:句柄(handle),PHP為此提供了parent關(guān)鍵字。
 
parent 調(diào)用父類的構(gòu)造方法

要引用一個(gè)類而不是對(duì)象的方法,可以使用 ::(兩個(gè)冒號(hào)),而不是 ->。

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

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


復(fù)制代碼 代碼如下:

<?php
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 賦傳進(jìn)來的值
$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;
}
}
 
?>


 
每個(gè)子類都會(huì)在設(shè)置自己的屬性前調(diào)用父類的構(gòu)造方法?;悾ǜ割悾┈F(xiàn)在僅知道自己的數(shù)據(jù),而我們也應(yīng)該盡量避免告訴父類任何關(guān)于子類的信息,這是一條經(jīng)驗(yàn)規(guī)則,大家想想如果某個(gè)子類的信息應(yīng)該是”保密“的,結(jié)果父類知道它的信息,其它子類可以繼承,這樣子類的信息就不保密了。

parent 調(diào)用父類被覆寫的方法

parent 關(guān)鍵字可以在任何覆寫父類的方法中使用。覆寫一個(gè)父類的方法時(shí),我們并不希望刪除父類的功能,而是拓展它,通過在當(dāng)前對(duì)象中調(diào)用父類的方法可以達(dá)到這個(gè)目的。
看看上面的代碼,可以發(fā)現(xiàn)兩個(gè)子類中 getSummaryLine() 方法中重復(fù)了許多代碼,我們應(yīng)該利用 ShopProduct 類中已經(jīng)存在的功能,而不是重復(fù)開發(fā):


復(fù)制代碼 代碼如下:

// 父類: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;
}


看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

名稱欄目:怎么在php中利用parent調(diào)用父類的構(gòu)造方法-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://muchs.cn/article48/degsep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、小程序開發(fā)、微信公眾號(hào)、軟件開發(fā)網(wǎng)站建設(shè)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)