PHP面向?qū)ο罄^承用法詳解(優(yōu)化與減少代碼重復)
本文實例講述了PHP面向?qū)ο罄^承用法。分享給大家供大家參考,具體如下:
繼承
先看兩個類
<?php class 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; // 看的頁數(shù) 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
點評:這兩個類,代碼重復性太高,有相同性,也有差異性。不如用繼承來簡化處理。
采用繼承來處理
<?php class 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
點評:繼承處理很好的解決了差異性,相通性問題。
進一步優(yōu)化處理
<?php class 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 ); // 繼承父類的構(gòu)造函數(shù) $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
點評:這里把共有屬性在父類中,其他個性屬性放在自己的類中處理。并設(shè)置自己的構(gòu)造方法,繼承父類的構(gòu)造方法。
進一步繼承父類的方法
<?php class 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
點評:同樣的結(jié)果,可以優(yōu)化優(yōu)化再優(yōu)化。這里繼承父類的方法。parent::getSummaryLine()。不過這個用的比較少。
繼續(xù)添加一些有意思的內(nèi)容
<?php class 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無效。
私有化屬性,通過方法來設(shè)置與獲取
<?php class ShopProduct { // 私有化屬性,通過方法來設(shè)置與獲取 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
點評:這里進一步私有化了屬性,要想獲取只能通過方法。這樣就確保了安全性。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP面向?qū)ο笕筇攸c學習(充分理解抽象、封裝、繼承、多態(tài))
- 詳解php中的類與對象(繼承)
- php面向?qū)ο笕ヂ?(七) 繼承性
- PHP入門教程之面向?qū)ο蟮奶匦苑治?繼承,多態(tài),接口,抽象類,抽象方法等)
- PHP面向?qū)ο蟪绦蛟O(shè)計高級特性詳解(接口,繼承,抽象類,析構(gòu),克隆等)
- PHP面向?qū)ο蟪绦蛟O(shè)計OOP繼承用法入門示例
- PHP面向?qū)ο蟪绦蛟O(shè)計繼承用法簡單示例
- PHP面向?qū)ο蟪绦蛟O(shè)計之接口的繼承定義與用法詳解
- PHP學習記錄之面向?qū)ο螅∣bject-oriented programming,OOP)基礎(chǔ)【類、對象、繼承等】
- PHP 對象繼承原理與簡單用法示例
相關(guān)文章
php中使用接口實現(xiàn)工廠設(shè)計模式的代碼
php實現(xiàn)工廠設(shè)計模式,使用接口實現(xiàn),表面上接口沒有什么用,因為php是類型自動轉(zhuǎn)換的。實現(xiàn)上使用接口可以約束類的定義,從而實現(xiàn)一致的訪問2012-06-06PHP Warning: Module ''modulename'' already loaded in問題解決辦法
這篇文章主要介紹了PHP Warning: Module 'modulename' already loaded in問題解決辦法,本文總結(jié)了兩種情況,需要的朋友可以參考下2015-03-03PHP 實現(xiàn)代碼復用的一個方法 traits新特性
這篇文章主要介紹了PHP 實現(xiàn)代碼復用的一個方法,traits的新特性的相關(guān)資料,需要的朋友可以參考下2015-02-02PHP實現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能示例
這篇文章主要介紹了PHP實現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能,涉及php頁面抓取、正則匹配、文件讀寫等相關(guān)操作技巧,需要的朋友可以參考下2019-06-06探討PHP中OO之靜態(tài)關(guān)鍵字以及類常量的詳解
本篇文章是對php中的靜態(tài)關(guān)鍵字以及類常量進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP常用排序算法實例小結(jié)【基本排序,冒泡排序,快速排序,插入排序】
這篇文章主要介紹了PHP常用排序算法,結(jié)合實例形式總結(jié)分析了php常見的排序算法,包括基本排序、冒泡排序、快速排序、插入排序等,需要的朋友可以參考下2017-02-02