首页 > 代码库 > 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();
?>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。