<?php
//通过abstract关键字定义一个抽象类
abstract class ACanEat{
//通过abstract关键字定一个不用具体实现的抽象方法eat()
abstract public function eat($food);
//抽象类中可以定义普通的方法,并具体实现
public function breath(){
echo "breath need air \n";
}
}
//同样用关键字extends关键字来继承抽象类
class Human extends ACanEat{
//继承抽象类的子类需要实现抽象类中定义的抽象方法
public function eat($food){
echo "human eating".$food."\n";
}
class Animal extends ACanEat{
public function eat($food){
echo "animal eating".$food."\n";
}
}
$man =new Human();
$man->eat('apple');
$man->breath();//从结果可以看出breath()方法是被Human类和Animal类公用的
$monkey = new Animal();
$monkey->eat('banana');
$monkey->breath();
输出结果如下
human eating apple
breath need air
animal eating banana
breath need air
?>