1 回答
TA贡献1842条经验 获得超21个赞
实际上,实例化保龄球类的过程,正如调用父级构造函数所暗示的那样parent::__construct();,将创建一个全新的板球类以及保龄球类。
因此,尝试访问此新创建的Cricket类的属性没有任何意义。
因此,当您实例化Bowler该类时,您还必须传递Cricket类成功构建所需的任何数据。
所以举个例子
<?php
class Cricket
{
protected $gameType;
function __construct($gameType)
{
$this->gameType=$gameType;
}
function display()
{
echo 'The cricket match is a ' . $this->gameType . " match";
}
}
class Bowler extends Cricket
{
public $type;
public $number;
function __construct($gameType, $type, $number)
{
$this->type=$type;
$this->number=$number;
parent::__construct($gameType);
}
function display()
{
parent:: display();
echo " with " . $this->number . " " . $this->type . " bowler";
}
}
$two = new Bowler('day-night', "left-hand","2");
$two->display();
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报