是否有任何明顯的區別爲什麼使用抽象擴展如果我們可以在普通類中做同樣的事情,除非它沒有提供例如。抽象類擴展和普通類之間的區別延伸
abstract class Survivalneeds {
abstract public function eat(); // everyone eats but different foods which would probably work as contract
public function breathe() {
// everyone inhale o2 exhale co2 only for animals
}
}
現在
class human extends Survivalneeds {
protected function eat() {
//sometimes eat goat
// contract
}
breathe()// already extending having same functionality inhale o2 and exhale co2
}
class goat extends Survivalneeds{
protected function eat() {
//wee eat greens
// contract
}
breathe()// already extending having same functionality inhale o2 and exhale co2
}
現在相同的功能,可以通過正常類由除合同法延長理所當然的合同,我們可以使用的界面也。
所以我們創造的抽象只是因爲它在這裏不可實例化? –
抽象方法也非常有用,檢查模板方法模式[link](https://sourcemaking.com/design_patterns/template_method) – Nautilus