<?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},{$this->producerFirstName}) ";
return $base;
}
}
/*抽象类,定义抽象类的方法在子类中实现*/
abstract class ShopProductWriter{
protected $products=array();
public function addProduct(ShopProduct $shopProduct){
$this->products[]=$shopProduct;
}
abstract public function write();
}
class TextProductWrite extends ShopProductWriter{
public function write(){
$str ="PRODUCTS:\n";
foreach($this->products as $shopProduct){
$str .= $shopProduct->getSummaryLine()."\n";
}
echo $str;
}
}
$shop=new ShopProduct('这是一个标题','你的姓','你的名字',100);
$text=new TextProductWrite();
$text->addProduct($shop);
$text->write();
?>
02-21
791