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

首頁 > 語言 > PHP > 正文

PHP面向對象繼承用法詳解(優化與減少代碼重復)

2024-05-04 23:53:11
字體:
來源:轉載
供稿:網友

本文實例講述了PHP面向對象繼承用法。分享給大家供大家參考,具體如下:

繼承

先看兩個類

<?phpclass CdProduct {  public $playLength; // 播放時間  public $title;  public $producerMainName;  public $producerFirstName;  public $price;  function __construct(  $title, $firstName,              $mainName, $price,              $playLength ) {    $this->title       = $title;    $this->producerFirstName = $firstName;    $this->producerMainName = $mainName;    $this->price       = $price;    $this->playLength    = $playLength;  }  function getPlayLength() {    return $this->playLength;  }  function getSummaryLine() {    $base = "{$this->title} ( {$this->producerMainName}, ";    $base .= "{$this->producerFirstName} )";    $base .= ": playing time - {$this->playLength}";    return $base;  }  function getProducer() {    return "{$this->producerFirstName}".        " {$this->producerMainName}";  }}class BookProduct {  public $numPages; // 看的頁數  public $title;  public $producerMainName;  public $producerFirstName;  public $price;  function __construct(  $title, $firstName,              $mainName, $price,              $numPages ) {    $this->title       = $title;    $this->producerFirstName = $firstName;    $this->producerMainName = $mainName;    $this->price       = $price;    $this->numPages     = $numPages;  }  function getNumberOfPages() {    return $this->numPages;  }  function getSummaryLine() {    $base = "{$this->title} ( {$this->producerMainName}, ";    $base .= "{$this->producerFirstName} )";    $base .= ": page count - {$this->numPages}";    return $base;  }  function getProducer() {    return "{$this->producerFirstName}".        " {$this->producerMainName}";  }}$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );print $product1->getSummaryLine();print "/n";$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );print $product2->getSummaryLine();print "/n";?>

輸出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

點評:這兩個類,代碼重復性太高,有相同性,也有差異性。不如用繼承來簡化處理。

采用繼承來處理

<?phpclass ShopProduct {  public $numPages;  public $playLength;  public $title;  public $producerMainName;  public $producerFirstName;  public $price;  function __construct(  $title, $firstName,              $mainName, $price,              $numPages=0, $playLength=0 ) {    $this->title       = $title;    $this->producerFirstName = $firstName;    $this->producerMainName = $mainName;    $this->price       = $price;    $this->numPages     = $numPages;    $this->playLength    = $playLength;  }  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 count - {$this->numPages}";    return $base;  }}$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, null, 50 );print $product1->getSummaryLine();print "/n";$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );print $product2->getSummaryLine();print "/n";?>

輸出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

點評:繼承處理很好的解決了差異性,相通性問題。

進一步優化處理

<?phpclass ShopProduct {  // 抽離出共有屬性  public $title;  public $producerMainName;  public $producerFirstName;  public $price;  function __construct(  $title, $firstName,              $mainName, $price ) {    $this->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 $playLength;  function __construct(  $title, $firstName,              $mainName, $price, $playLength ) {    parent::__construct(  $title, $firstName,                $mainName, $price ); // 繼承父類的構造函數    $this->playLength = $playLength; // 處理自己專有的屬性  }  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 count - $this->numPages";    return $base;  }}$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );print $product1->getSummaryLine();print "/n";$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );print $product2->getSummaryLine();print "/n";?>

輸出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

點評:這里把共有屬性在父類中,其他個性屬性放在自己的類中處理。并設置自己的構造方法,繼承父類的構造方法。

進一步繼承父類的方法

<?phpclass ShopProduct {  public $title;  public $producerMainName;  public $producerFirstName;  public $price;  function __construct(  $title, $firstName,              $mainName, $price ) {    $this->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 $playLength;  function __construct(  $title, $firstName,              $mainName, $price, $playLength ) {    parent::__construct(  $title, $firstName,                $mainName, $price );    $this->playLength = $playLength;  }  function getPlayLength() {    return $this->playLength;  }  function getSummaryLine() {    $base = parent::getSummaryLine();    $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 = parent::getSummaryLine();    $base .= ": page count - {$this->numPages}";    return $base;  }}$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );print $product1->getSummaryLine();print "/n";$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );print $product2->getSummaryLine();print "/n";?>

輸出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

點評:同樣的結果,可以優化優化再優化。這里繼承父類的方法。parent::getSummaryLine()。不過這個用的比較少。

繼續添加一些有意思的內容

<?phpclass ShopProduct {  private $title;  private $discount = 0;  private $producerMainName;  private $producerFirstName;  protected $price;  function __construct(  $title, $firstName,              $mainName, $price ) {    $this->title       = $title;    $this->producerFirstName = $firstName;    $this->producerMainName = $mainName;    $this->price       = $price;  }  function setDiscount( $num ) {    $this->discount=$num;  }  function getPrice() {    return ($this->price - $this->discount);  }  function getProducer() {    return "{$this->producerFirstName}".        " {$this->producerMainName}";  }  function getSummaryLine() {    $base = "{$this->title} ( {$this->producerMainName}, ";    $base .= "{$this->producerFirstName} )";    return $base;  }}class CdProduct extends ShopProduct {  public $playLength;  function __construct(  $title, $firstName,              $mainName, $price, $playLength ) {    parent::__construct(  $title, $firstName,                $mainName, $price );    $this->playLength = $playLength;  }  function getPlayLength() {    return $this->playLength;  }  function getSummaryLine() {    $base = parent::getSummaryLine();    $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 getPrice() {    return $this->price;  }  function getNumberOfPages() {    return $this->numPages;  }  function getSummaryLine() {    $base = parent::getSummaryLine();    $base .= ": page count - {$this->numPages}";    return $base;  }}$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );$product1->setDiscount( 3 );print $product1->getSummaryLine();print "/n";print "price: {$product1->getPrice()}/n";$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );$product2->setDiscount( 3 ); // 折扣對book無效print $product2->getSummaryLine();print "/n";print "price: {$product2->getPrice()}/n";?>

輸出:

cd1 ( bobbleson, bob ): playing time - 50
price: 1
book1 ( harrelson, harry ): page count - 30
price: 4

點評:父類添加了折扣,book繼承之后,修改了getPrice方法,所以折扣對book無效。

私有化屬性,通過方法來設置與獲取

<?phpclass ShopProduct {  // 私有化屬性,通過方法來設置與獲取  private $title;  private $producerMainName;  private $producerFirstName;  protected $price;  private $discount = 0;  public function __construct(  $title, $firstName,              $mainName, $price ) {    $this->title       = $title;    $this->producerFirstName = $firstName;    $this->producerMainName = $mainName;    $this->price       = $price;  }  public function getProducerFirstName() {    return $this->producerFirstName;  }  public function getProducerMainName() {    return $this->producerMainName;  }  public function setDiscount( $num ) {    $this->discount=$num;  }  public function getDiscount() {    return $this->discount;  }  public function getTitle() {    return $this->title;  }  public function getPrice() {    return ($this->price - $this->discount);  }  public function getProducer() {    return "{$this->producerFirstName}".        " {$this->producerMainName}";  }  public function getSummaryLine() {    $base = "{$this->title} ( {$this->producerMainName}, ";    $base .= "{$this->producerFirstName} )";    return $base;  }}class CdProduct extends ShopProduct {  private $playLength = 0;  public function __construct(  $title, $firstName,              $mainName, $price, $playLength ) {    parent::__construct(  $title, $firstName,                $mainName, $price );    $this->playLength = $playLength;  }  public function getPlayLength() {    return $this->playLength;  }  public function getSummaryLine() {    $base = parent::getSummaryLine();    $base .= ": playing time - {$this->playLength}";    return $base;  }}class BookProduct extends ShopProduct {  private $numPages = 0;  public function __construct(  $title, $firstName,              $mainName, $price, $numPages ) {    parent::__construct(  $title, $firstName,                $mainName, $price );    $this->numPages = $numPages;  }  public function getNumberOfPages() {    return $this->numPages;  }  public function getSummaryLine() {    $base = parent::getSummaryLine();    $base .= ": page count - {$this->numPages}";    return $base;  }  public function getPrice() {    return $this->price;  }}$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );print $product1->getSummaryLine()."/n";$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );print $product2->getSummaryLine()."/n";?>

輸出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

點評:這里進一步私有化了屬性,要想獲取只能通過方法。這樣就確保了安全性。

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 张家界市| 利津县| 米林县| 日照市| 望城县| 都江堰市| 福安市| 梧州市| 连山| 海伦市| 宁都县| 伊吾县| 大邑县| 塔城市| 仲巴县| 深圳市| 襄垣县| 鲁甸县| 连云港市| 高碑店市| 平凉市| 乌兰县| 连江县| 湖州市| 滨海县| 天气| 昔阳县| 兴化市| 大石桥市| 黄山市| 察雅县| 施秉县| 义马市| 曲周县| 永登县| 宁城县| 碌曲县| 安乡县| 翁源县| 寿光市| 德清县|