PHP面向对象

<html>
<head>
    <title>index</title>
</head>
<body>
<p>PHP面向对象</p>
<?php
class animal{
    public $name  = "";
    public $color = "";
    public $age = "";
    public static $sex="雄性";

     function __construct($name,$color,$age)
    {
        $this->name=$name;
        $this->color=$color;
        $this->age=$age;
    }

    function __destruct()
    {
        // 析构方法,在对象销毁前调用
        echo  '&nbsp;&nbsp;再见,'.$this->name;
    }

    function getInformation(){
        return $this->name;
    }

    function setInformation($name){
        return $this->name=$name;
    }

    function getAnimalInfo(){
        echo "动物名: ".$this->name.",动物颜色:".$this->color.",动物年龄:".$this->age;
    }
}

class bird extends animal{
    var $swing;

    function fly(){
        echo "我可以飞翔。";
    }
}

echo animal::$sex;
$bird=new bird();

$bird->setInformation("鹦鹉");
$bird->fly();
$bird->getInformation();

$ianimal=new animal('野猪','黑色','10岁');
$ianimal->getAnimalInfo();

$pig=new animal();
$dog=new animal();

$pig->setInformation("猪");
$dog->setInformation("狗");
echo $pig->getInformation()."<br>".$dog->getInformation();

?>

</body>
</html>











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人海中的海盗

你的打赏将是对我的激励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值