PHP之abstract

來源:互聯網
上載者:User

標籤:nbsp   pre   實現   不能   abs   抽象類別   name   類的方法   abstract   

 1 <?php 2     //抽象類別 : 用 abstract修飾的類 或 方法 。稱為 抽象類別或者抽象方法。 3     //也不能執行個體化。 4     //抽象方法沒有方法體,,只有方法的聲明。 5     //抽象方法必須存在抽象類別中。 6     //子類繼承抽象類別 必須實現其抽象方法,也就是重寫。 7     //使用抽象方法的原因: 就是子類 一定存在,並且 在不同的實現, 8     abstract class Goods{ 9         public $_name;10         public function __construct($_name){11             $this->_name=$_name;    12         }13         public abstract function getName();14     }15 16     class Book extends Goods{17         public $_author;18 19         //實現其父類的方法體20         public function getName(){21             $this->_name=$_name;22             $this->_author=$_author;23 24         }25     }26 27     class Phone extends Goods{28         public $_brand;29         //實現其父類的方法體30         public function __construct($_name, $_brand){31             parent::__construct($_name);32             $this->_brand=$_brand;33         }34 35         public function getName(){36             $this->_name=$_name;37             $this->_brand=$_brand;38         }39     }40 41     //執行個體化對象42     $book = new Book(‘php從入門到精通‘,‘mingrikeji‘);43     $book->getName();44     $p = new Phone(‘aa‘,‘aa‘);45     $p->getName();

 

PHP之abstract

聯繫我們

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