php物件導向:類的繼承執行個體講解

來源:互聯網
上載者:User
什麼是類的繼承?說白了,我覺得就是提高代碼使用效率的。下面我就給大家介紹下繼承。

類的繼承概念

子類繼承父類的所有成員變數個方法,包括構造方法,當子類被執行個體化時,php會現在子類中查詢構造方法,如果子類有自己的構造方法,php會先調用子類中的方法;當子類中沒有時,php則去調用父類中的構造方法,這也就是我們說的繼承。

類的繼承是通過關鍵字extends,文法為:

class A extends B{...}

A代表子類,B代表父類。

好了,瞭解了基本概念,我們就看看類的繼承執行個體吧

首先建立一個類,類中有不同的方法:

<?php//父類class Lol{public $name;public $type;public $price;public function __construct($name,$price){$this->name = $name;$this->price = $price;}function ShowInfo(){echo  "在這不顯示";}}//子類Playclass Play extends Lol{           //定義子類,繼承父類public $type;                     //在子類中定義變數public function __construct($name,$type){$this->name = $name;$this->type = $type;}function ShowInfo(){if($this->type == "mid"){return  $this->name . "會玩這個位置";}else{return $this->name . "不會玩這個位置";}}}//執行個體化對象$player = new Play("faker","mid");echo $player->ShowInfo();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.