1.抽象類
抽象類機(jī)制中總是要定義一個(gè)公共的基類,而將特定的細(xì)節(jié)留給繼承者來(lái)實(shí)現(xiàn)。通過(guò)抽象概念,可以在開(kāi)發(fā)項(xiàng)目中創(chuàng)建擴(kuò)展性很好的架構(gòu)。任何一個(gè)類,如果它里面至少有一個(gè)方法是被聲明為抽象的,那么這個(gè)類就必須被聲明為抽象的。被定義為抽象的方法只是聲明了其調(diào)用方式(參數(shù)),不能定義其具體的功能實(shí)現(xiàn)。在類的聲明中使用 abstract 修飾符就可以將某個(gè)類聲明為抽象的。
1.1方法原型(prototype)
是指方法的定義中剔除了方法體之后的簽名。它包括存取級(jí)別、函數(shù)關(guān)鍵字、函數(shù)名稱和參數(shù)。他不包含({})或者括號(hào)內(nèi)部的任何代碼。例如下面的代碼就是一個(gè)方法原型:
代碼如下:
public function prototypename($protoparam)
繼承一個(gè)抽象類的時(shí)候,子類必須定義父類中的所有抽象方法;另外,這些方法的訪問(wèn)控制必須和父類中一樣(或者更為寬松)。
1.2關(guān)于抽象類
某個(gè)類只要包含至少一個(gè)抽象方法就必須聲明為抽象類
聲明為抽象的方法,在實(shí)現(xiàn)的時(shí)候必須包含相同的或者更低的訪問(wèn)級(jí)別。
不能使用 new 關(guān)鍵字創(chuàng)建抽象類的實(shí)例。
被生命為抽象的方法不能包含函數(shù)體。
如果將擴(kuò)展的類也聲明為抽象類,在擴(kuò)展抽象類時(shí),可以不用實(shí)現(xiàn)所有的抽象方法。(如果某個(gè)類從抽象類繼承,當(dāng)它沒(méi)有實(shí)現(xiàn)基類中所聲明的所有抽象方法時(shí),它就必須也被聲明為抽象的。)
1.3使用抽象類
代碼如下:
<?php
abstract class car
{
abstract function getmaxspeend();
}
class roadster extends car
{
public $speend;
public function setspeend($speend = 0)
{
$this->speend = $speend;
}
public function getmaxspeend()
{
return $this->speend;
}
}
class street
{
public $cars ;
public $speendlimit ;
function __construct( $speendlimit = 200)
{
$this -> speendlimit = $speendlimit;
$this -> cars = array();
}
protected function isstreetlegal($car)
{
if ($car->getmaxspeend() < $this -> speendlimit)
{
return true;
}
else
{
return false;
}
}
public function addcar($car)
{
if($this->isstreetlegal($car))
{
echo 'the car was allowed on the road.';
$this->cars[] = $car;
}
else
{
echo 'the car is too fast and was not allowed on the road.';
}
}
}
$porsche911 = new roadster();
$porsche911->setspeend(340);
$fuwaistreet = new street(80);
$fuwaistreet->addcar($porsche911);
/**
*
* @result
*
* the car is too fast and was not allowed on the road.[finished in 0.1s]
*
*/
?>
2.對(duì)象接口
使用接口(interface),可以指定某個(gè)類必須實(shí)現(xiàn)哪些方法,但不需要定義這些方法的具體內(nèi)容。
接口是通過(guò) interface 關(guān)鍵字來(lái)定義的,就像定義一個(gè)標(biāo)準(zhǔn)的類一樣,但其中定義所有的方法都是空的。
接口中定義的所有方法都必須是公有,這是接口的特性。
接口是一種類似于類的結(jié)構(gòu),可用于聲明實(shí)現(xiàn)類所必須聲明的方法。例如,接口通常用來(lái)聲明api,而不用定義如何實(shí)現(xiàn)這個(gè)api。
大多數(shù)開(kāi)發(fā)人員選擇在接口名稱前加上大寫字母i作為前綴,以便在代碼和生成的文檔中將其與類區(qū)別開(kāi)來(lái)。
2.1使用接口
和集成抽象類需要使用 extends 關(guān)鍵字不同的是,實(shí)現(xiàn)接口使用的是 implements 關(guān)鍵字。一個(gè)類可以實(shí)現(xiàn)多個(gè)接口,這時(shí),我們需要用逗號(hào)將他們隔開(kāi)。如果將某個(gè)類標(biāo)記為實(shí)現(xiàn)了某個(gè)接口,但卻沒(méi)有實(shí)現(xiàn)這個(gè)借口的所有方法,將會(huì)拋出錯(cuò)誤。
2.2使用接口的案例
代碼如下:
<?php
abstract class car
{
abstract function setspeend($speend = 0);
}
interface ispeendinfo
{
function getmaxspeend();
}
class roadster extends car implements ispeendinfo
{
public $speend;
public function setspeend($speend = 0)
{
$this->speend = $speend;
}
public function getmaxspeend()
{
return $this->speend;
}
}
class street
{
public $cars ;
public $speendlimit ;
function __construct( $speendlimit = 200)
{
$this -> speendlimit = $speendlimit;
$this -> cars = array();
}
protected function isstreetlegal($car)
{
if ($car->getmaxspeend() < $this -> speendlimit)
{
return true;
}
else
{
return false;
}
}
public function addcar($car)
{
if($this->isstreetlegal($car))
{
echo 'the car was allowed on the road.';
$this->cars[] = $car;
}
else
{
echo 'the car is too fast and was not allowed on the road.';
}
}
}
$porsche911 = new roadster();
$porsche911->setspeend(340);
$fuwaistreet = new street(80);
$fuwaistreet->addcar($porsche911);
/**
*
* @result
*
* the car is too fast and was not allowed on the road.[finished in 0.1s]
*
*/
?>
3.instanceof 操作符
instanceof 操作符是php5中的一個(gè)比較操作符。他接受左右兩邊的參數(shù),并返回一個(gè)boolean值。這個(gè)操作符是用來(lái)確定對(duì)象的某個(gè)實(shí)例是否為特定的類型,或者是否從某個(gè)類型繼承,又或者實(shí)現(xiàn)類某個(gè)特定的接口。
代碼如下:
echo $porsche911 instanceof car;
//result:1
echo $porsche911 instanceof ispeendinfo;
//result:1
4.契約式編程
契約式編程是指在編寫類之前實(shí)現(xiàn)聲明接口的一種編程實(shí)踐。這種方法在保證類的封裝性方面非常有用。使用契約式編程技術(shù),我們可以在創(chuàng)建應(yīng)用程序之前定義出視圖實(shí)現(xiàn)的功能,這和建筑師在修建大樓之前先畫好藍(lán)圖的做法非常相似。
5.總結(jié)
抽象類是使用 abstract 關(guān)鍵字聲明的類。通過(guò)將某個(gè)類標(biāo)記為抽象類,我們可以推遲實(shí)現(xiàn)所聲明的方法。要將某個(gè)方法聲明為抽象方法,只要去掉包含所有大括號(hào)的方法實(shí)體,將方法聲明的代碼行用分號(hào)結(jié)束即可。
抽象類不能直接實(shí)例化,他們必須被繼承。
如果某個(gè)類從抽象類繼承,當(dāng)它沒(méi)有實(shí)現(xiàn)基類中所聲明的所有抽象方法時(shí),它就必須也被聲明為抽象的。
在接口中,我們可以聲明沒(méi)有方法體的方法原型,這點(diǎn)與抽象類很相似。他們之間的區(qū)別在于,接口不能聲明任何具有方法體的方法;并且他們使用的語(yǔ)法也不一樣。為了將揭開(kāi)規(guī)則強(qiáng)制加到某個(gè)類上,我們需要使用implements關(guān)鍵字,而不是extends關(guān)鍵字。
有些情況下我們需要確定某個(gè)類是否是特定類的類型,或者是否實(shí)現(xiàn)了特定的接口。 instanceof 分成適合完成這個(gè)任務(wù)。instanceof 檢查三件事情:實(shí)例是否是某個(gè)特定的類型,實(shí)例是否從某個(gè)特定的類型繼承,實(shí)例或者他的任何祖先類是否實(shí)現(xiàn)類特定的接口。
某些語(yǔ)言具有從多個(gè)類繼承的能力,這稱為多重繼承。php不支持多重繼承。想法,他提供了為一個(gè)類聲明多個(gè)接口的功能。
接口在聲明類必須遵循的規(guī)則時(shí)非常有用。契約式編程技術(shù)使用這一功能來(lái)增強(qiáng)封裝性,優(yōu)化工作流。
更多信息請(qǐng)查看IT技術(shù)專欄