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