您的位置:首页 > 其它

通过继承实现多态(小学生日常)

2016-07-18 12:07 387 查看

<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2016/7/18
* Time: 9:41
*/
//通过继承实现多态
class father{
public $name;
public function getWeight(){
return"59kg";
}
public function setName(){
$this->name="father";//this 调用本身
}
}
class son extends father{
public function setName()
{
// parent::setName(); // TODO: Change the autogenerated stub
$this->name="son";
}
}
//上段注释,输出father。
class son1 extends father{
public function getWeight()
{
return parent::getWeight(); // TODO: Change the autogenerated stub
}
}
$son=new son();
$son->setName();
echo $son->name."<br>";

$weight=new son1();
$weight->getWeight();
echo $weight->getWeight();


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: