我有这个抽象类
abstract class Guitar {
protected $strings;
public function __construct($no_of_strings) {
$this->strings = $no_of_strings;
echo 'Guitar class constructor is called <br/>';
}
abstract function play();
}
还有儿童班,
class Box_Guitar extends Guitar {
public function __construct($no_of_strings) {
echo 'Box Guitar constructor is called <br/>';
$this->strings = $strings + 100;
}
public function play() {
echo 'strumming ' . $this->strings;
}
}
然后我开始上课,
$box_guitar = new Box_Guitar(6);
我的输出是
Box Guitar 构造函数被调用
吉他类构造函数被调用
弹奏 106
所以我的问题是为什么要调用父构造函数?我没有使用 Parent::__construct()。