我有这样的结构:
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 "
You have bought ".$android2->get_description()." for $".$android2->cost();我明白了
You have bought Unkown brand plus a long lasting battery, for $650
再次:
我想知道为什么我会从get_description()调用的HTC中取得Unknown brand而不是PLACEHOLDER_FOR_CODE_4,而$650正在正确计算。