PHP物件導向編程--繼承(重寫(多態)、封裝、可見度)

來源:互聯網
上載者:User

PHP除了簡單的為了實現特定功能外,也可以實現物件導向的編程方式,而且也可以很優雅。
下面利用具體的代碼執行個體,講述物件導向中的繼承等相關概念。

<?php/** * Author: helen * CreateTime: 2016/4/10 9:58 * description: 物件導向--繼承(重寫(多態)、封裝、可見度) */namespace Home\Controller\Program;use Home\Controller\CommonController;class InheritController extends CommonController{    //物件導向編程測試方法    public function main(){        /*         * instanceof關鍵詞可以用來查看一個特別的對象是不是屬於一個特定的類的類型         * */        $dog = new Dog('helen');        $cat = new Cat('huihui');        $dog->eat();        $cat->eat();        $dog->sleep();        $cat->sleep();        $dog->fetch();        $cat->climb();        $dog->play();        $cat->play();        unset($dog,$cat);        echo '<br>';        $side = 40;        $square = new Square($side);        echo $square->getArea();        unset($square);        echo '<br>';        $parent = new Test();        $child = new LittleTest();        $parent->printVar('public');        $child->printVar('public');        $parent->public = 'modified';        $parent->printVar('public');        $child->printVar('public');        $parent->printVar('protected');        $child->printVar('protected');        $parent->printVar('private');        $child->printVar('private');        $this->test();        $this->test();        $this->test();        $obj = new SomeClass();        echo $obj instanceof SomeClass; //輸出1        if($obj instanceof SomeClass){            echo 'true';        }else{            echo 'false';        }        echo $obj::$counter;        $obj1 = new SomeClass();        echo $obj1::$counter;    }    function test(){        static $n = 0;        echo "$n<br>";        $n++;    }}//父類class ParentClass{}//繼承類/* * 繼承建構函式和解構函式 * 在PHP中,建立一個子類的對象的時候,父類的建構函式不會被自動調用 * * *//* * 重寫(創造了多態) * 子類定義的方法必須和父類的方法具有完全相同的名稱和參數數量(參數數量不同的技術稱為方法的“重載”) * 定義為final的方法不能被任何子類所重寫 * *//* * 存取控制(封裝) * 1、PHP中必須指定屬性的可見度(預設public)。 * 2、public,可以從任何一個地方訪問:類本身內部、派生的子類和其他類。 * 3、protected,只能在類本身以及子類中訪問。 * 4、private,只能在類本身訪問(私人的變數名通常以一個底線開始) * 有效避免一些bug和一些不必要的行為(比如,改變類的屬性的能力) * *//* * 範圍解析運算子(::) * 1、用於在類中(而不是對象)訪問成員 * 2、在使用類的時候,在父類和子類具有相同的屬性和方法時,利用它可以避免混淆。 * 3、在類外的時候,在沒有建立對象的情況下使用該操作符訪問類的成員。 * 4、在類外使用類名;在類中,使用self關鍵字。 * 5、要引用父類中的一個成員,可以使用parent關鍵字和範圍解析操作符來引用。 * *//* * 靜態成員 * 1、類裡的靜態屬性相當於函數中的靜態變數。 * 2、一個靜態函數變數能夠在每次函數被調用的時候記住其值。 * 3、類的靜態成員的概念也一樣,添加static關鍵字。 * 4、靜態屬性,不能在類裡使用$this訪問,應該使用self後跟範圍解析操作符(::),後面是帶著貨幣符號的變數名。 * *//* * 類常數 * 1、類常數和靜態屬性是一樣的,它能夠被類(或者其子類)的全部執行個體訪問。 * 2、值是不能改變的。 * 3、建立類常數的方法是使用const關鍵字,後面跟著常數名(沒有貨幣符號)。 * 4、不能通過對象進行訪問,應該使用$obj::PI * 5、可以在任何地方使用ClassName::CONSTANT_NAME,類中使用self::CONSTANT_NAME * */class ChildClass extends ParentClass{}//demo樣本class Pet{    public $name;    function __construct($pet_name){        $this->name = $pet_name;        self::sleep();    }    function eat(){        echo "<p>$this->name is eating.</p>";    }    function sleep(){        echo "<p>$this->name is sleeping.</p>";    }    function play(){        echo "<p>$this->name is playing.</p>";    }}class Cat extends Pet{    function climb(){        echo "<p>$this->name is climbing.</p>";    }    function play(){        parent::play();        echo "<p>$this->name is climbing.</p>";    }}class Dog extends Pet{    function fetch(){        echo "<p>$this->name is fetching.</p>";    }    function play(){        parent::play();        echo "<p>$this->name is fetching.</p>";    }}//Rectangle類class Rectangle{    public $width=0;    public $height=0;    function __construct($width=0,$height=0){        $this->width=$width;        $this->height=$height;    }    function setSize($width,$height){        $this->width=$width;        $this->height=$height;    }    function getArea(){        return ($this->width*$this->height);    }    function getPerimeter(){        return (($this->width+$this->height)*2);    }    function isSquare(){        if($this->width==$this->height){            return true;        }else{            return false;        }    }}class Square extends Rectangle{    function __construct($side=0){        $this->width = $side;        $this->height = $side;    }}class Test{    public $public = 'public';    protected $protected = 'protected';    private $private = 'private';    function printVar($var){        echo "<p>In Test,\$$var:.'{$this->$var}'.</p>";    }}class LittleTest extends Test{    function printVar($var){        echo "<p>In LittleTest,\$$var:.'{$this->$var}'.</p>";    }}class SomeClass{    public static $counter = 0;    function __construct(){        self::$counter++;    }}

編程是一門技術,更是一門藝術。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.