我有这个结构:
abstract class Android {
public function __construct() {
}
public abstract function cost();
public function get_model(){
return "unkown model";
}
}
class HTC extends Android {
public function __construct() {
parent::__construct();
$this->model= "HTC";
}
public function get_model() {
return $this->model;
}
public function cost() {
return 500;
}
}
abstract class FeaturesDecorator extends Android {
public function __construct() {
parent::__construct();
}
public abstract function get_description();
}
class VideoCamera extends FeaturesDecorator {
private $android;
public function __construct($android) {
parent::__construct();
$this->android=$android;
}
public function get_description() {
return $this->android->get_model()." plus a video camera, ";
}
public function cost() {
return 200 + $this->android->cost();
}
}
class BigScreenSize extends FeaturesDecorator {
private $android;
public function __construct($android) {
parent::__construct();
$this->android=$android;
}
public function get_description() {
return $this->android->get_model()." plus a big size screen, ";
}
public function cost() {
return 100 + $this->android->cost();
}
}
应用以下模式后:
$android2 = new HTC();
$android2 = new BigScreenSize($android2);
$android2 = new LongLastingBattery($android2);
echo "<br/><br/>You have bought ".$android2->get_description()." for $".$android2->cost();
我明白了
您以 650 美元的价格购买了 Unkown 品牌和长效电池
再次:
我想知道为什么我得到Unknown brand
而不是HTC
fromget_model()
调用get_description()
,而$650
被正确计算。