PHP物件導向,PHP繼承在php的相關操作中顯得特別重要,本文將會詳細的解說其相關的內容。
<!--?php class 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->number=$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; } } ?>
本文詳解了PHP物件導向,PHP繼承相關程式碼的知識,更多相關內容請關注php中文網。
相關推薦:
以上是解說PHP面向對象,PHP繼承相關程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!