讲解PHP面向对象,PHP继承相关代码

jacklove
Lepaskan: 2023-03-30 18:58:02
asal
2328 orang telah melayarinya

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;
        }
Salin selepas log masuk
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;
        }
    }
?>
Salin selepas log masuk

本文详解了PHP面向对象,PHP继承相关代码的知识,更多相关内容请关注php中文网。

相关推荐:

讲解PHP面向对象序列化、反序列化的相关代码

如何通过PHP方法判断是否是手机登录(代码)

如何通过php循环和递归实现无限级分类

Atas ialah kandungan terperinci 讲解PHP面向对象,PHP继承相关代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!