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
相關文章
相關標籤/搜索