php如何使用類繼承解決代碼重復(fù)的問題-創(chuàng)新互聯(lián)

小編給大家分享一下php如何使用類繼承解決代碼重復(fù)的問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

在祿豐等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),成都全網(wǎng)營(yíng)銷推廣,外貿(mào)網(wǎng)站制作,祿豐網(wǎng)站建設(shè)費(fèi)用合理。

具體分析如下:

繼承直白地說就是給一個(gè)類建一個(gè)或多個(gè)子類,要?jiǎng)?chuàng)建子類就必須在類聲明中使用 extends 關(guān)鍵字,新類名在前,extends 在中,父類名在后。 
下例中,我們創(chuàng)建兩個(gè)新類,BookProduct 和Cdproduct ,它們都繼承自 ShopProduct 類。


<?php
header('Content-type:text/html;charset=utf-8');
// 從這篇開始,類名首字母一律大寫,規(guī)范寫法
class ShopProduct{    // 聲明類
 public $numPages;   // 聲明屬性
 public $playLenth;
 public $title; 
 public $producerMainName;
 public $producerFirstName;
 public $price;
 function __construct($title,$firstName,$mainName,$price,$numPages=0,$playLenth=0){
  $this -> title = $title;    // 給屬性 title 賦傳進(jìn)來的值
  $this -> producerFirstName= $firstName; 
  $this -> producerMainName = $mainName; 
  $this -> price= $price; 
  $this -> numPages= $numPages; 
  $this -> playLenth= $playLenth; 
 }
 function getProducer(){    // 聲明方法
  return "{$this -> producerFirstName }"."{$this -> producerMainName}";
 }
 function getSummaryLine(){
  $base = "{$this->title}( {$this->producerMainName},";
  $base .= "{$this->producerFirstName} )";
  return $base;
 }
}
class CdProduct extends ShopProduct {
 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 {
 function getNumberOfPages(){
  return $this -> numPages;
 }
 function getSummaryLine(){
  $base = "{$this->title}( {$this->producerMainName},";
  $base .= "{$this->producerFirstName} )";
  $base .= ":page cont - {$this->numPages} )";
  return $base;
 }
}
?>

由于子類沒有定義構(gòu)造方法,所以在實(shí)例化 BookProduct 和Cdproduct 類時(shí),會(huì)自動(dòng)調(diào)用父類 ShopProduct 的構(gòu)造方法。

子類默認(rèn)繼承了父類所有的 public 和 protected方法與屬性(但沒有繼承 private 方法與屬性,后面會(huì)講到這三個(gè)關(guān)鍵字的作用)。也就是說,我們可以在從 Cdproduct 類實(shí)例化的對(duì)象中調(diào)用 getProducer() 方法,盡管 getProducer() 是在 ShopProduct 類中定義的。
將一下代碼加到上面:


$product2 = new CdProduct("PHP面向?qū)ο?quot;,"郭","碗瓢盆",7,null,"7小時(shí)");
print "美好生活:{$product2 -> getProducer()}<br>";
// 結(jié)果是:美好生活:郭碗瓢盆

這兩個(gè)子類都繼承了父類的公共部分,但注意, BookProduct 和Cdproduct 類都覆寫了 getSummaryLine() 方法,提供了自己獨(dú)特的實(shí)現(xiàn),說明子類可以拓展和修改父類的功能。

但該方法在父類中的實(shí)現(xiàn)似乎有點(diǎn)多余,因?yàn)樗膬蓚€(gè)子類都重寫了該方法,不過其他子類可能會(huì)用到它的基本功能。該方法的存在為客戶端代碼提供了保證:所有的 ShopProduct 對(duì)象都將有 getSummaryLine() 方法, BookProduct 和Cdproduct 都使用各自的 getSummaryLine() 方法訪問 $title 屬性。
可能一開始,繼承是一個(gè)不太容易理解的概念。首先我們可以知道,通過定義一個(gè)從其他類繼承而來的類,我們確保一個(gè)類擁有其自由的功能和父類的功能。然后就是子類的“搜索”功能,當(dāng)我們調(diào)用 $product2 -> getProducer() 時(shí),在 CdProduct 類中并沒有找到 getProducer() 方法,那么就查找 ShopProduct 類中是否有這個(gè)方法,有就調(diào)用,沒有則報(bào)錯(cuò)。對(duì)屬性的訪問也是同樣的道理。
看看 ShopProduct 的構(gòu)造方法,就會(huì)發(fā)現(xiàn)我們?nèi)匀辉?基類(父類)中管理本應(yīng)是子類處理的數(shù)據(jù):BookProduct 應(yīng)該處理 $numPages 參數(shù)和屬性;Cdproduct 應(yīng)該處理 $playLength 參數(shù)和屬性。要完成這個(gè)工作,我們需要在子類中分別定義構(gòu)造方法。

以上是“php如何使用類繼承解決代碼重復(fù)的問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

名稱欄目:php如何使用類繼承解決代碼重復(fù)的問題-創(chuàng)新互聯(lián)
鏈接分享:http://muchs.cn/article20/dhocjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、App設(shè)計(jì)、網(wǎng)站內(nèi)鏈、商城網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(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)

成都網(wǎng)頁(yè)設(shè)計(jì)公司