<?php header('content-type:text/html;charset=utf-8'); /* 通过abstract关键字定义抽象类,当类中至少有一个方法为抽象方法的时候,这个类就必须定义成抽象类 抽象类不能直接被实例化,只相当于定义了调用的方式,并没有具体的实现 子类的继承的时候必须实现所有的抽象方法 子类在实现抽象方法的时候,可见性可以和父类相同或者宽松 */ abstract class Pet{ public $name; abstract public function eat(); abstract public function sleep(); public function breath(){ echo '呼吸新鲜空气...<br/>'; } } class Cat extends Pet{ public function __construct($name){ $this->name=$name; } public function eat(){ echo $this->name.' is eating...<br/>'; } public function sleep(){ echo $this->name.' is sleeping...<br/>'; }
} class Dog extends Pet{ public function __construct($name){ $this->name=$name; } public function eat(){ echo $this->name.' is eating...<br/>'; } public function sleep(){ echo $this->name.' is sleeping...<br/>'; } }