我试图在扩展类中使用显示函数,该类首先在父类中获取显示函数。但是,它不会在echo语句中显示变量。游戏类型(在这种情况下为“一日”)不会显示。
<?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($type,$number)
{
$this->type=$type;
$this->number=$number;
parent::__construct($this->gameType);
}
function display()
{
parent:: display();
echo " with " . $this->number . " " . $this->type . " bowler";
}
}
$one = new Cricket("day-night");
$one->display();
echo'<br>';
$two = new Cricket("day-night");
$two = new Bowler("left-hand","2");
$two->display();
?>
茅侃侃