这些差异既有理论性的,也有实践性的:
- interface 是对您的类拥有和宣传的一些能力的描述(因此实现相同接口的各种类可以以相同的方式使用)
- 抽象类可以是一个默认实现,包含可能出现在所有实现中的部分。它不必实现完整的接口
示例 - 一个接口:
// define what any class implementing this must be capable of
interface IRetrieveData {
// retrieve the resource
function fetch($url);
// get the result of the retrieval (true on success, false otherwise)
function getOperationResult();
// what is this class called?
function getMyClassName();
}
现在我们有了一组要求,这些要求将针对每个实现它的类进行检查。让我们创建一个抽象类及其子类:
// define default behavior for the children of this class
abstract class AbstractRetriever implements IRetrieveData {
protected $result = false;
// define here, so we don't need to define this in every implementation
function getResult() {
return $result;
}
// note we're not implementing the other two methods,
// as this will be very different for each class.
}
class CurlRetriever extends AbstractRetriever {
function fetch($url) {
// (setup, config etc...)
$out = curl_execute();
$this->result = !(curl_error());
return $out;
}
function getMyClassName() {
return 'CurlRetriever is my name!';
}
}
class PhpRetriever extends AbstractRetriever {
function fetch($url) {
$out = file_get_contents($url);
$this->result = ($out !== FALSE);
return $out;
}
function getMyClassName() {
return 'PhpRetriever';
}
}
一个完全不同的抽象类(与接口无关),有一个实现我们接口的子类:
abstract class AbstractDog {
function bark() {
return 'Woof!';
}
}
class GoldenRetriever extends AbstractDog implements IRetrieveData {
// this class has a completely different implementation
// than AbstractRetriever
// so it doesn't make sense to extend AbstractRetriever
// however, we need to implement all the methods of the interface
private $hasFetched = false;
function getResult() {
return $this->hasFetched;
}
function fetch($url) {
// (some retrieval code etc...)
$this->hasFetched = true;
return $response;
}
function getMyClassName() {
return parent::bark();
}
}
现在,在其他代码中,我们可以这样做:
function getStuff(IRetrieveData $retriever, $url) {
$stuff = $retriever->fetch($url);
}
而且我们不必担心将传入哪些检索器(cURL、PHP 或 Golden),以及它们将如何实现目标,因为所有检索器都应该具有相似的行为。你也可以使用抽象类来做到这一点,但是你会根据类的祖先而不是它的能力来限制自己。