仰望星空望
2015-09-10 15:36
<?php
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set("PRC");
class Human
{
public $name;
public $weight;
//受保护的类成员,只有自身和其子类可以访问到
protected $height;
//也是受保护的成员,只能被自身访问
private $isHungry=true;
public function eat($food)
{
echo $this->name."'s eating ".$food."<br>";
}
}
class NbaPlayer extends Human
{
public $team='';
public $PlayNum='';
//受保护的类成员,只能别自身访问到,当public function getAge()自身定义了这个方法时,就可以被自身访问到
private $age='50';
function __construct($name,$height,$weight,$team,$PlayNum)
{
echo"construct方法被调用<br>";
$this->name=$name;
$this->height=$height;
$this->weight=$weight;
$this->team=$team;
$this->PlayNum=$PlayNum;
echo $this->isHungry."\n";
}
function __destruct()
{ echo"destruct方法被调用.$this->name.<br>";
}
//定义方法[跟原来定义函数是一样的]
public function run()
{
echo'Running<br>';
}
public function jump()
{
echo'Jumping<br>';
}
//受保护的类成员age可以被自身调用
public function getAge()
{
echo $this->name."'is age is ".($this->age)."\n";
}
}
$wang=new NbaPlayer("wang","160cm","50kg","Bull","56");
//echo $wang->getAge()."\n";
//echo $wang->height;//提示不能访问
?>
你打开php.ini配置文件 搜索error_reporting =
将后面的值改为E_ALL
然后就会报notice错误
construct方法被调用
Notice: Undefined property: NbaPlayer::$isHungry in D:\phpStudy\WWW\test.php on line 33
destruct方法被调用.wang.
然而我的也没有提示-。-我还把php.ini里面的display_error打开了……
private 属性和方法不会被子类继承,只能在自身里面访问
PHP面向对象编程
70154 学习 · 361 问题
相似问题