首页 > 代码库 > php继承与实现作比较 还有 final 、static的讲解

php继承与实现作比较 还有 final 、static的讲解

一个实例来说明继承与实现

一个猴子一生下来就继承了父亲的爬树功能,但是它又想像鸟一样可以飞,像鱼一样可以在水里游。所以

它就必须实现这两种动物的功能。

用代码表示

<?php

      

/********实现与继承的比较********/

class Monkey{

public $name;

public $age;

public function climbing($name){
$this->name=$name;
echo $this->name."会爬树";

}

}

 


interface bird{

public function fly();
}
interface fish{

public function swim();
}
class LittleMonkey extends Monkey implements bird,fish{

function __construct($name){
$this->climbing($name);//调用父类方法
}
function fly(){
echo "会飞";
}
function swim(){
echo "会游";
}
}
/**

一个猴子一生下来就继承了父亲的爬树功能,但是它又想像鸟一样可以飞,像鱼一样可以在水里游。所以

它就必须实现这两种动物的功能。

**/
$test=new LittleMonkey("孙悟空");
$test->fly();
$test->swim();

?>