php使用類繼承解決代碼重複的問題_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php使用類繼承解決代碼重複的問題。分享給大家供大家參考。具體分析如下:

繼承直白地說就是給一個類建一個或多個子類,要建立子類就必須在類聲明中使用 extends 關鍵字,新類名在前,extends 在中,父類名在後。
 
下例中,我們建立兩個新類,BookProduct 和Cdproduct ,它們都繼承自 ShopProduct 類。

複製代碼 代碼如下:
<?php
header('Content-type:text/html;charset=utf-8');
// 從這篇開始,類名首字母一律大寫,規範寫法
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 賦傳進來的值
  $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;
 }
}
?>


 
由於子類沒有定義構造方法,所以在執行個體化 BookProduct 和Cdproduct 類時,會自動調用父類 ShopProduct 的構造方法。

子類預設繼承了父類所有的 public 和 protected方法與屬性(但沒有繼承 private 方法與屬性,後面會講到這三個關鍵字的作用)。也就是說,我們可以在從 Cdproduct 類執行個體化的對象中調用 getProducer() 方法,儘管 getProducer() 是在 ShopProduct 類中定義的。
 
將一下代碼加到上面:

複製代碼 代碼如下:
$product2 = new CdProduct("PHP物件導向","郭","碗瓢盆",7,null,"7小時");
print "美好生活:{$product2 -> getProducer()}<br>";
// 結果是:美好生活:郭碗瓢盆

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

但該方法在父類中的實現似乎有點多餘,因為它的兩個子類都重寫了該方法,不過其他子類可能會用到它的準系統。該方法的存在為用戶端代碼提供了保證:所有的 ShopProduct 對象都將有 getSummaryLine() 方法, BookProduct 和Cdproduct 都使用各自的 getSummaryLine() 方法訪問 $title 屬性。
 
可能一開始,繼承是一個不太容易理解的概念。首先我們可以知道,通過定義一個從其他類繼承而來的類,我們確保一個類擁有其自由的功能和父類的功能。然後就是子類的“搜尋”功能,當我們調用 $product2 -> getProducer() 時,在 CdProduct 類中並沒有找到 getProducer() 方法,那麼就尋找 ShopProduct 類中是否有這個方法,有就調用,沒有則報錯。對屬性的訪問也是同樣的道理。
 
看看 ShopProduct 的構造方法,就會發現我們仍然在 基類(父類)中管理本應是子類處理的資料:BookProduct 應該處理 $numPages 參數和屬性;Cdproduct 應該處理 $playLength 參數和屬性。要完成這個工作,我們需要在子類中分別定義構造方法。

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.