引入 2018-11-24 13:54

打印子类的信息(更完整),所以要重写。

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

class Animal{

	protected $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 $tiao_height;
	public function __construct($name,$age,$color,$tiao_height){
		parent::__construct($name,$age,$color);
		$this->tiao_height=$tiao_height;
		echo 'Cat<br />';
	}

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

}

// 鸟的类型
class Bird extends Animal{
	public $fei_sudu;

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

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

}

// ------------------------------------------

// $a=new Animal('一只动物',1,'蓝色');
// $a->show();
// echo $a->name;

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

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



多态的意义!

PHP的多态的做法要比C++的要简介,


Tool::get_infos();

Tool::get_infos();

同样的代码,传递不同的(子类)实参,得到不同的结果状态(多态)(各个子类自己实现的同名函数里面的东西)!


1:继承

2:虚函数(PHP模式已经是需函数)

3:父类引用作为形参,传递的是各个子类类型的对象(类型不一样);

4:子类中覆盖父类的同名函数;

结果:

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

class Animal{

	protected $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 $tiao_height;
	public function __construct($name,$age,$color,$tiao_height){
		parent::__construct($name,$age,$color);
		$this->tiao_height=$tiao_height;
		echo 'Cat<br />';
	}

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

}


// 鸟的类型
class Bird extends Animal{
	public $fei_sudu;

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

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

}

// 青蛙的类型
class QingWa extends Animal{
	public $youyong_shengdu;

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

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

}

// ------------------------------------------
// 我的工具类
class Tool{
	public static function get_infos($a){
		$a->show();
	}
}

// $a=new Animal('一只动物',1,'蓝色');
// $a->show();
// echo $a->name;

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

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

Tool::get_infos($c);
Tool::get_infos($b);

$qw=new QingWa('一只青蛙',5,'青色',6);
Tool::get_infos($qw);