解說PHP面向對象,PHP繼承相關程式碼

jacklove
發布: 2023-03-30 18:58:02
原創
2346 人瀏覽過

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循環和遞迴實現無限級分類

以上是解說PHP面向對象,PHP繼承相關程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板