PHP面向对象继承用法详解(优化与减少代码重复)
作者:牛逼的霍啸林 发布时间:2023-11-21 10:16:48
本文实例讲述了PHP面向对象继承用法。分享给大家供大家参考,具体如下:
继承
先看两个类
<?php
class CdProduct {
public $playLength; // 播放时间
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; // 看的页数
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
点评:这两个类,代码重复性太高,有相同性,也有差异性。不如用继承来简化处理。
采用继承来处理
<?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
点评:继承处理很好的解决了差异性,相通性问题。
进一步优化处理
<?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 = "{$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
点评:这里把共有属性在父类中,其他个性属性放在自己的类中处理。并设置自己的构造方法,继承父类的构造方法。
进一步继承父类的方法
<?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
点评:同样的结果,可以优化优化再优化。这里继承父类的方法。parent::getSummaryLine()。不过这个用的比较少。
继续添加一些有意思的内容
<?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 ); // 折扣对book无效
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
点评:父类添加了折扣,book继承之后,修改了getPrice方法,所以折扣对book无效。
私有化属性,通过方法来设置与获取
<?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->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
点评:这里进一步私有化了属性,要想获取只能通过方法。这样就确保了安全性。
希望本文所述对大家PHP程序设计有所帮助。
猜你喜欢
- 1. interface{}初探Go是强类型语言,各个实例变量的类型信息正是存放在interface{}中的,Go中的反射也与其底层结构有关
- 项目环境:python3.6,django2.1接口功能: 将传入参数a和b字符串相加,返回结果1.新建一个django项目# 新建一个名为
- 简单定义图轴:import numpy as npimport matplotlib.pyplot as plt创建一个简单的matplot
- Python中Math库和Python库都具备求对数的函数。import numpy as npimport math1. Numpy库1.
- 本文实例讲述了Python实现通过解析域名获取ip地址的方法。分享给大家供大家参考,具体如下:从网上查找的一些资料,特此做个笔记案例1:de
- 导言在前两章的做的DataList的例子里我们都是使用单列的HTML<table>来显示数据.而自定义使DataList将数据显
- 项目演示:一、输入金额二、跳转到支付宝付款三、支付成功四、跳转回自己网站在使用支付宝接口的前期准备:1、支付宝公钥2、应用公钥3、应用私钥4
- 本文实例讲述了Python3读取zip文件信息的方法。分享给大家供大家参考。具体实现方法如下:该程序接受一个字符串,其内容是一个zip文件,
- 方法超级简单,把时间格式化一下就好了,直接奉上代码function transDate() { var $time
- 当然首先得去下载ASPupload 程序,安装后使用!官方网站下载:http://www.aspupload.com/使用ASP实现文件上载
- 一.一维数组的转置描述一维数组的重塑就是将一行或一列的数组转换为多行多列的数组重塑之后的数组应于原有数组形状兼容(数组元素应该相等)用法和参
- 静态方法不需要所在类被实例化就可以直接使用。静态方法效率上要比实例化高,静态方法的缺点是不自动进行销毁,而实例化的则可以做销毁。静态方法和静
- 安装:cnpm i -S jwt-decode引入:import jwt_decode from "jwt-decode"
- <asp:ImageButton ID="BtnMailaddress" runat="server&q
- 最近几天从网上找了几个asp.net的登录案例想要研究研究代码,结果在用Sql Server2005附加数据库文件时弹出错误信息:看到网友回
- 在Python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文件,下一篇文章
- 前言pygame是用来开发游戏的一套基于SDL的模板,它可以是python创建完全界面化的游戏和多媒体程序,而且它基本上可以在任何系统上运行
- 一、使用matplotlib显示图import matplotlib.pyplot as plt #plt用于显示图片import matp
- 有的时候,我们在网页中会用到复选框,也就是多选框,当用户提交输入信息的时候我们会获取复选框的内容,然后保存到数据库中,如经常用到的是用户输入
- 描述如下: 用mysqldump 导出 Trigger 的时候遇到一个问题,贴出来,以免大家犯错。 在执行下面的操作时: [root@ytt