<?php //基本
尺度
CD类 class CD { public $tracks = array(); public $band = ''; public $title = ''; public function __construct() {} public function setTitle($title) { $this->title = $title; } public function setBand($band) { $this->band = $band; } public function addTrack($track) { $this->tracks[] = $track; } } //加强
型CD类, 与尺度
CD的独一
分歧
是写至CD的第一个track是数据track("DATA TRACK") class enhadcedCD { public $tracks = array(); public $band = ''; public $title = ''; public function __construct() { $this->tracks = "DATA TRACK"; } public function setTitle($title) { $this->title = $title; } public function setBand($band) { $this->band = $band; } public function addTrack($track) { $this->tracks[] = $track; } } //CD工场
类,完成
对以上两个类详细
实例化操作 class CDFactory { public static function create($type) { $class = strtolower($type) . "CD"; return new $class; } } //实例操作 $type = "enhadced"; $cd = CDFactory::create($type); $tracksFromExternalSource = array("What It Means", "Brr", "Goodbye"); $cd->setBand("Never Again"); $cd->setTitle("Waste of a Rib"); foreach ($tracksFromExternalSource as $track) { $cd->addTrack($track); } ?>