继承
继承是从一个基类得到一个或多个类的机制。
继承自另一个类的类被称为该类的子类。这种关系通常用父亲和孩子来比喻。子类将继承父
类的特性。这些特性由属性和方法组成。子类可以增加父类(也称为超类, superclass))之外的新
功能,因此子类也被称为父类的“扩展”。
在深入学习继承的语法之前,我们先了解一下它可以帮助我们解决什么问题
使用继承
要创建一个子类,必须在类声明中使用 extends关键字。在本例中,我们创建了两个新类BookProduct和 CdProduct,它们都继承自 ShopProduct类。由于派生类没有定义构造方法,所以在实例化时会自动调用父类的构造方法。子类默认继承了父类所有的public和 protecte方法(但是没有继承 private方法或属性)。也就是说,我们可以在从 CdProduct类实例化的对象中调用 getProducer()方法,即使 getProducer()是在 ShopProduct类中定义的。
$product2-> new CdProduct("Exile on Col harbour Lane"The","Alabama 310.99.mu1l.60.33);
print" artist: ( $product2->get Producer())\n";
这样两个子类都继承了父类的公共部分。我们可以把BookProduct对象当做一种 ShopProduc对象,传递BookProduct或 CdProduct对象给 ShopProductwriter类的 write()方法来开展需要的工作。注意 CdProduct和 BookProduct类都覆写了getSummaryLine()方法,提供自己独特的实现。派生类可以扩展和修改父类的功能,同时还继承父类的属性。 BookProduct和 CdProduct都在它们的getSummaryLine()中访问了 title属性可能一开始,继承是一个不太容易理解的概念。通过定义一个从其他类继承面来的类,我们确保一个类拥有其自有的功能和父类的功能。另一种可以帮助你理解继承的思路是“搜索”。当我们调用 $product2->getProducer()时,在 CdProduct类中并没有找到这样的方法,那么就查找ShopProduct中的默认实现。另一方面,当调用 $product2->getSummaryLine()时,在 CdProduct中
ShopProduct.php
<?php
class ShopProduct{
private $title;
private $producerFirstName;
private $producerMainName;
private $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;
}
public function getProducer(){
return "{$this->producerFirstName}"."{$this->producerMainName}";
}
function getSummaryLine(){
$base="$this->title({$this->producerMainName},{$this->producerFirstName})";
return $base;
}
}
CdProduct.php
<?php
class CdProduct extends ShopProduct{
private $playLength=0;
public 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;
}
}
BookProduct.php
<?php
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;
}
}
ShopProductWriter.php
<?php
class ShopProductWriter{
public $products=array();
public function addProduct( ShopProduct $shopProduct ){
$this->products[]=$shopProduct;
}
public function write(){
$str="";
foreach ($this->products as $shopProduct){
$str.="{$shopProduct->title}:";
$str.=$shopProduct->getProducer();
$str.="({$shopProduct->getPrice()})\n";
}
print $str;
}
}