您的位置 首页 PHP

PHP面向对象继承用法详解(优化与减少代码重复)

本文实例讲述了PHP面向对象继承用法。分享给大家供大家参考,具体如下:

继承

先看两个类

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,$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”,30 );
print $product2->getSummaryLine();
print “\n”;
?>

输出:

cd1 ( bobbleson,bob ): playing time – 50
book1 ( harrelson,harry ): page count – 30

点评:这两个类,代码重复性太高,有相同性,也有差异性。不如用继承来简化处理。

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”,null,harry ): page count – 30

点评:继承处理很好的解决了差异性,相通性问题。

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,$playLength ) {
parent::__construct( $title,$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,$numPages ) {
parent::__construct( $title,$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”,harry ): page count – 30

点评:这里把共有属性在父类中,其他个性属性放在自己的类中处理。并设置自己的构造方法,继承父类的构造方法。

关于作者: dawei

【声明】:金华站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

热门文章