<?php
/**
* 【多态】
* 定义一个抽象类:Tiger,有两个子类:XTiger 和 MTiger
*/
header("Content-type: text/html; charset=utf-8");
/*父类*/
abstract class Tiger{
public abstract function climb();
}
/*两个子类,继承父类*/
class XTiger extends Tiger{
public function climb(){
echo '摔下来<br>';
}
}
class MTiger extends Tiger{
public function climb(){
echo '爬到树顶<br>';
}
}
/*调用类*/
class Client{
public static function call($animal){
$animal->climb();
}
}
Client::call(new XTiger()); //输出:“摔下来”
Client::call(new MTiger()); //输出:“爬到树顶”
//----------------------------------------------------
class Cat{
public function climb(){
echo '飞到天上';
}
}
Client::call(new Cat()); //输出:“飞到天上”