php接口(interface)跟抽象类abstract的作用与好处

假设现在有六个类

  1. 动物(Animal)抽象类
  2. 老虎(tiger) 继承动物类
  3. 蛇类(snake) 继承动物类
  4. 兔子(rabbit) 继承动物类
  5. 农夫(farmer)农夫可以喂养Animal

贴代码跟解释

abstract class Animal{ //定义一个抽象类animal 

	public function move($destination){}//动物移动
	public function drink(){}//动物喝水
}

//三个动物 tiger snake rabbit

class tiger extends Anima{
	public function getname(){
		return 'tiger';
	}

	public function move($destination){
		echo $this->getname().' move to '.$destination.'<br>';
	}

	public function drink(){
		echo $this->getname().' drink water <br>';
	}

	public function hunt($animal){
		echo $this->getname().' hunt '.$animal."<br>";
	}
}

class snake extends Animal{
	public function getname(){
		return 'snake';
	}

	public function move($destination){
		echo  $this->getname().' move to '.$destination.'<br>';
	}

	public function drink(){
		echo $this->getname().' drink water <br>';
	}

	public function hunt($animal){
		echo $this->getname().' hunt '.$animal."<br>";
	}
}

class rabbit extends Animal{
	public function getname(){
		return 'rabbit';
	}

	public function move($destination){
		echo $this->getname().' move to '.$destination."<br>";
	}

	public function drink(){
		echo $this->getname().' drink water <br>';
	}
}

//农夫喂水

class farmer{
	public function feedwater(Animal $animal,$destination){
		$animal->move($destination);
		$animal->drink();
	} 

	public function feedanimal(Animal $animal,$destination){
		$animal->hunt($destination);
	}
}
$farmer=new farmer();

$farmer->feedwater(new tiger(),'room');

$farmer->feedwater(new snake(),'grassland');

$farmer->feedwater(new rabbit(),'kitchen');

$farmer->feedanimal(new tiger(),'animal');

$farmer->feedanimal(new snake(),'animal');

使用接口类

<?php

interface Animal{ 

	public function move($destination);//动物移动
	public function drink();//动物喝水
}
//tiger snake实现捕食方法 但是rabbit不存在这个方法 所以需要再另外定义一个接口类
//引用接口
interface Hunt{
	public function hunt($animal);
}
class tiger  implements Animal,Hunt{
	public function getname(){
		return 'tiger';
	}

	public function move($destination){
		echo $this->getname().' move to '.$destination.'<br>';
	}

	public function drink(){
		echo $this->getname().' drink water <br>';
	}

	public function hunt($animal){
		echo $this->getname().' hunt '.$animal."<br>";
	}
}

class snake  implements Animal{
	public function getname(){
		return 'snake';
	}

	public function move($destination){
		echo  $this->getname().' move to '.$destination.'<br>';
	}

	public function drink(){
		echo $this->getname().' drink water <br>';
	}

	public function hunt($animal){
		echo $this->getname().' hunt '.$animal."<br>";
	}
}

class rabbit implements Animal{
	public function getname(){
		return 'rabbit';
	}

	public function move($destination){
		echo $this->getname().' move to '.$destination."<br>";
	}

	public function drink(){
		echo $this->getname().' drink water <br>';
	}
}

//农夫喂水

class farmer{
	public function feedwater(Animal $animal,$destination){
		$animal->move($destination);
		$animal->drink();
	} 

	public function feedanimal(Animal $animal,$destination){
		$animal->hunt($destination);
	}
}

$farmer=new farmer();

$farmer->feedwater(new tiger(),'room');

$farmer->feedwater(new snake(),'grassland');

$farmer->feedwater(new rabbit(),'kitchen');

$farmer->feedanimal(new tiger(),'animal');

$farmer->feedanimal(new snake(),'animal');

写法是类似的
abstract跟interface的区别
1.abstract 可以有成员变量 可以实现方法
2.interface可以实现继承多个接口

上述的接口类hunt 可以不要 但是为了说明多继承还是加上了

参考链接https://blog.csdn.net/JLongSL/article/details/54885780#commentBox

猜你喜欢

转载自blog.csdn.net/qq_36899235/article/details/84190492
今日推荐