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();