祁望
2016-05-12 09:22
<p><?php
class Human
{
public $name;
public $height;
public $weight;
public function eat($food){
echo $this ->name."'s eating ".$food."\n";
}
}
class Player extends Human
{
}
$jor=new Human("jor","198cm","75kg");
echo $jor ->name."\n";
$jor ->eat("orange");
?>
你new错了,要new Player类
<?php
class Human{
public $name;
public $height;
public $weight;
public function eat($food){
echo $this->name."'s eating ".$food."\n";
}
}
class Player extends Human{
function __construct($name,$height,$weight){
$this->name=$name;
$this->height=$height;
$this->weight=$weight;
}
}
$jor = new Player("jor","198cm","75kg");
echo $jor->name."\n";
$jor->eat("orange");
?>
new 子类不是父类
可不可以帮我把代码直接改好啊?我把构造函数加入Human类里,name也没出来
构造函数加到Human类里面
<p><?php
class Human
{
public $name;
public $height;
public $weight;
public function eat($food){
echo $this ->name."'s eating ".$food."\n";
}
}
class Player extends Human
{
function __construct($name,$height,$weight)
$this->name=$name;
$this->height=$height;
$this->weight=$weight;
}
$jor=new Human("jor","198cm","75kg");
echo $jor ->name."\n";
$jor ->eat("orange");
?>
加了构造函数还是没显示
你没写构造函数,你的数据没有录进去$jor里面,$jor用的是你定义的类里面的原来的name height weight 就是空的
PHP面向对象编程
70153 学习 · 361 问题
相似问题