//在抽象方法前面添加abstract关键字可以表明这个方法是抽象方法 不需要具体实现 public abstract function eat($food); //抽象类中可以包含普通方法,有方法的具体实现。 public function breath() { echo "Breath use the air \n <br/>"; } } //继承抽象类的关键字是extends class Human extends ACanEat { //继承抽象类的子类需要实现抽象类中定义的抽象的方法 public function eat($food) { echo "Human eating " . $food . "<br/>"; } } class Animal extends ACanEat { public function eat($food) { echo "Animal eating " . $food . "<br/>"; } } $man = new Human(); $man->breath(); $man->eat("apple"); $animal = new Animal(); $animal->breath(); $animal->eat("bananer");
// 抽象方法需要在子类中实现 public abstract function eat($food); public function breath() { echo "Breath use the air.\n"; } } // Human类实现了ICanEat接口 class Human extends ACanEat { // 跟Animal类的实现是不同的 public function eat($food) { echo "Human eating " . $food . "\n"; } } // Animal类实现了ICanEat接口 class Animal extends ACanEat { public function eat($food) { echo "Animal eating " . $food . "\n"; } } $man = new Human(); $man->eat("Apple"); $man->breath(); // 和Animal共用了抽象类ICanEat的breath方法 $monkey = new Animal(); $monkey->eat("Banana"); $monkey->breath();