PHP接口(interface)和抽象類(abstract)


interface

定義了一個接口類,它里面的方法其子類必須實現。接口是類的一個模板,其子類必須實現接口中定義的所有方法。

interface User{

    function getHeight($height);

    function getWeight($weight); 

}

class my implements User{

    function getHeight($username){

        echo $height;

    }

    function getWeight($weight){

        echo $weight;

    }

}

abstract

抽象類就把類像的部分抽出來,就是把重復的東西寫到抽象類中,減少工作量。只要方法沒有用abstract聲明,在其子類中就不用實現。而且在子類中該方法為公共方法。

abstract User{

    abstract function getHeight($height);

    function getWeight(){

        echo $weight;

    }

}

class my extends User{

    function getHeight($height){

         echo $height;

    }

    function getInfoById($id){

        $this->getWeight.“<br/>”;

        echo $id;

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM