同名覆盖 2018-11-24 13:11

代码:

<?php
header('Content-type:text/html;charset=utf-8');

class Animal{

	public $name;
	public $age;
	public $color;

	public function __construct($name,$age,$color){
		$this->name=$name;
		$this->age=$age;
		$this->color=$color;
		echo 'Animal<br />';
	}

	public function set_name($name){
		$this->name=$name;
	}

	public function set_age($age){
		$this->age=$age;
	}

	public function set_color($color){
		$this->color=$color;
	}

	// 接口
	public function get_name(){
		return $this->name;
	}

	// 接口
	public function get_age(){
		return $this->age;
	}

	// 接口
	public function get_color(){
		return $this->color;
	}

	public function show(){
		echo "名字:{$this->name}<br />";
		echo "年龄:{$this->age}岁<br />";
		echo "颜色:{$this->color}<br />";
	}

}

// 猫的类型
class Cat extends Animal{
	public function __construct($name,$age,$color){
		parent::__construct($name,$age,$color);
		echo 'Cat<br />';
	}

	public function show(){
		echo "我的名字:{$this->name}<br />";
		echo "我的年龄:{$this->age}岁<br />";
		echo "我的颜色:{$this->color}<br />";
	}
}

// 鸟的类型
class Bird extends Animal{
	public function __construct($name,$age,$color){
		parent::__construct($name,$age,$color);
		echo 'Bird<br />';
	}
}

// ------------------------------------------
$c=new Cat('一只猫',2,'灰色');
$c->show();

$b=new Bird('猫头鹰',3,'白色');
$b->show();