我只是寫這樣的代碼:抽象類方法聲明
<?php
class test
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
class testabs extends test{
public function getValue()
{
}
public function prefixValue($f)
{
}
}
$obj = new testabs();
?>
當我運行這段代碼,我收到以下錯誤:
Fatal error: Class test contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (test::getValue, test::prefixValue) in C:\wamp64\www\study\abstract.php on line 12
我明白了這個錯誤的第一部分。我將課堂測試改爲抽象,錯誤消失了,但or
部分我無法理解。
這個_or_部分是從一個類擴展你的'test'類的角度來看的。它是雙向的;或者讓你的'test'類抽象或者讓任何其他類擴展'test'抽象,因爲它們沒有實現任何繼承的抽象聲明。 – dbf