PHP实现多继承 - 经过接口的多继承特性(二)

原文地址:http://small.aiweimeng.top/index.php/archives/51.htmlphp

在上篇文章中写到php可使用```Trait```实现代码的复用,
下面介绍使用接口的多继承特性实现代码的复用;html

示例代码:htm

header("Content-type:text/html;charset=utf8");
interface TestOne{

    function test_one();
}

interface TestTwo{

    function test_two($name);

}

//接口间多继承使用,隔开
interface TestThree extends TestOne,TestTwo{

    function test_three($name);

}


class World implements TestThree
{

    function test_one()
    {
        // TODO: Implement test_one() method.
        echo '使用php接口实现多继承,必须调用接口中的全部方法</br>';
    }

    function test_two($name)
    {
        // TODO: Implement test_two() method.
        echo $name.'<br/>';
    }

    function test_three($name)
    {
        // TODO: Implement test_three() method.
        echo "{$name}<br/>";
    }

}


$obj = new World();
$obj->test_one();
$obj->test_two("two");
$obj->test_three('three');

  

运行结果:blog

使用php接口实现多继承,必须调用接口中的全部方法
two
three
相关文章
相关标签/搜索