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

jacklove
发布: 2023-03-30 18:58:02
原创
2328 人浏览过

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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!