<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 ' 再见,'.$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>
PHP面向对象
于 2021-07-22 17:37:07 首次发布