標籤: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