1. 程式人生 > >php介面(interface)跟抽象類abstract的作用與好處

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