这段代码提示Fatal error: Uncaught Error: Cannot call constructor 怎么解决给出答案<?php class human{ public $name="";//姓名 public $height="";//身高 public $weight="";//体重 public function eat($food){ echo $this->name."在吃".$food."<br/>"; } } class anchors extends human{ public $name=""; public $stagename=""; public function __construct($name,$stagename){ parent::__construct(); $this->name=$name; $this->stagename=$stagename; } public function singing(){ echo"我是女主播,我会唱歌<br/>"; } public function eat($food){ echo $this->name."是女主播,我是边唱歌边吃($food)<br/>"; } } class nbaplayer extends human{ public $team="";//团队 public $playername="";//球员号码 public function __construct($name,$height,$weight,$team,$playername){ parent::__construct(); $this->name=$name; $this->height=$height; $this->weight=$weight; $this->team=$team; $this->playername=$playername; echo "构造函数执行了,当前对象是{$this->name}<br/>"; } public function run(){//跑步 echo "跑步中<br/>"; } public function jump(){//跳跃 echo "跳跃<br/>"; } public function dribble(){ echo "运球<br/>";//运球 } public function pass(){ echo "传球<br/>";//传球 } public function dunk(){ echo "扣篮<br/>";//扣篮 } public function eat($food){ echo "我是nba球员,我是站着吃饭,边吃($food)变看球赛<br/>"; } } $jordon=new nbaplayer("乔丹","1.98米","98公斤","公牛","23"); //输出乔丹对象 echo "名称=".$jordon->name."<br/>"; echo $jordon->eat("苹果"); $linda=new anchors("琳达","LD"); echo $linda->eat("苹果"); ?>
时间: 2024-03-21 17:39:37 浏览: 168
php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法
在类 `human` 中没有定义构造函数,所以在继承 `human` 的子类 `anchors` 和 `nbaplayer` 中调用父类构造函数时,应该传递对应的参数。修改代码如下:
```
<?php
class human{
public $name="";//姓名
public $height="";//身高
public $weight="";//体重
public function eat($food){
echo $this->name."在吃".$food."<br/>";
}
}
class anchors extends human{
public $stagename="";//艺名
public function __construct($name,$stagename){
parent::__construct();
$this->name=$name;
$this->stagename=$stagename;
}
public function singing(){
echo"我是女主播,我会唱歌<br/>";
}
public function eat($food){
echo $this->name."是女主播,我是边唱歌边吃($food)<br/>";
}
}
class nbaplayer extends human{
public $team="";//团队
public $playername="";//球员号码
public function __construct($name,$height,$weight,$team,$playername){
parent::__construct();
$this->name=$name;
$this->height=$height;
$this->weight=$weight;
$this->team=$team;
$this->playername=$playername;
echo "构造函数执行了,当前对象是{$this->name}<br/>";
}
public function run(){//跑步
echo "跑步中<br/>";
}
public function jump(){//跳跃
echo "跳跃<br/>";
}
public function dribble(){
echo "运球<br/>";//运球
}
public function pass(){
echo "传球<br/>";//传球
}
public function dunk(){
echo "扣篮<br/>";//扣篮
}
public function eat($food){
echo "我是nba球员,我是站着吃饭,边吃($food)变看球赛<br/>";
}
}
$jordon=new nbaplayer("乔丹","1.98米","98公斤","公牛","23"); //输出乔丹对象
echo "名称=".$jordon->name."<br/>";
echo $jordon->eat("苹果");
$linda=new anchors("琳达","LD");
echo $linda->eat("苹果");
?>
```
阅读全文