php __construct 传参问题 20
<?phpclassPeople{var$name;protected$age;private$tel;function__construct($name,$age,$t...
<?php
class People
{
var $name;
protected $age;
private $tel;
function __construct($name,$age,$tel)
{
$this->name = $name;
$this->age = $age;
$this->tel = $tel;
}
function eat()
{
echo $this->name."吃饭";
}
function sleep()
{
echo $this->name."睡觉";
}
function run()
{
echo $this->name."走路";
}
}
class student extends People
{
public $scores;
function __construct($num)
{
$this->scores = $num;
}
}
$std = new student() //这里应该怎么传参数才正确,为什么?
?>
<?php
class People
{
var $name;
protected $age;
private $tel;
function __construct($name,$age,$tel)
{
$this->name = $name;
$this->age = $age;
$this->tel = $tel;
}
function eat()
{
echo $this->name."吃饭";
}
function sleep()
{
echo $this->name."睡觉";
}
function run()
{
echo $this->name."走路";
}
}
class student extends People
{
public $scores;
function __construct($name,$age,$tel,$num)
{
parent::__construct($name,$age,$tel);
$this->scores = $num;
echo $this->name."<br>";
}
}
$std =new student("张三",18,234234234,"32421nba");
echo $stu->name;//这句为什么打印不出来?
?> 展开
class People
{
var $name;
protected $age;
private $tel;
function __construct($name,$age,$tel)
{
$this->name = $name;
$this->age = $age;
$this->tel = $tel;
}
function eat()
{
echo $this->name."吃饭";
}
function sleep()
{
echo $this->name."睡觉";
}
function run()
{
echo $this->name."走路";
}
}
class student extends People
{
public $scores;
function __construct($num)
{
$this->scores = $num;
}
}
$std = new student() //这里应该怎么传参数才正确,为什么?
?>
<?php
class People
{
var $name;
protected $age;
private $tel;
function __construct($name,$age,$tel)
{
$this->name = $name;
$this->age = $age;
$this->tel = $tel;
}
function eat()
{
echo $this->name."吃饭";
}
function sleep()
{
echo $this->name."睡觉";
}
function run()
{
echo $this->name."走路";
}
}
class student extends People
{
public $scores;
function __construct($name,$age,$tel,$num)
{
parent::__construct($name,$age,$tel);
$this->scores = $num;
echo $this->name."<br>";
}
}
$std =new student("张三",18,234234234,"32421nba");
echo $stu->name;//这句为什么打印不出来?
?> 展开
展开全部
$std = new student(100);//传入scores
另外student的构造函数应该这样写
function __construct($num)
{
parent::__construct('someone',24,'123445');//调用父类的构造函数完成初始化
$this->scores = $num;
}
至于父类的构造函数参数可以从student类传进来,像这样
function __construct($name,$age,$tel,$num)
或者只有一个参数的话使用数组传入,像这样
function __construct($arg)
{
parent::__construct($arg['name'],$arg['age'],$arg['tel']);//调用父类的构造函数完成初始化
$this->scores = $arg['num'];
}
echo $stu->name;//这句为什么打印不出来?
是$std吧?
另外student的构造函数应该这样写
function __construct($num)
{
parent::__construct('someone',24,'123445');//调用父类的构造函数完成初始化
$this->scores = $num;
}
至于父类的构造函数参数可以从student类传进来,像这样
function __construct($name,$age,$tel,$num)
或者只有一个参数的话使用数组传入,像这样
function __construct($arg)
{
parent::__construct($arg['name'],$arg['age'],$arg['tel']);//调用父类的构造函数完成初始化
$this->scores = $arg['num'];
}
echo $stu->name;//这句为什么打印不出来?
是$std吧?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你在实例化的时候写的$std
echo的时候写 $stu当然出不来……
echo的时候写 $stu当然出不来……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你实例化的对象是$std 下面用的时候是$stu 肯定不会输出来啊。、
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
echo $stu->name;//这句为什么打印不出来?
我汗,没见你实例化 $stu,在别的文件么?
我汗,没见你实例化 $stu,在别的文件么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询