[英]PHP Constructors: Will an “inherited” constructor use Inherited or Parent class overriden methods?
假設我有一堂課
class Item {
public function __construct($id) {
if(!empty($id)) {
$this->doSomethingWithID($id);
}
}
public function dummyMethod() {
//does Something.
}
protected function doSomethingWithID($id) {
//Does something with the ID
}
}
如果我有這樣的繼承類:
class Product extends Item {
public function __construct() {
parent::__construct();
}
protected function doSomethingWithID($id) {
//OVERRIDES THE PARENT FUNCTION
}
}
產品類會使用重寫的方法嗎? 還是會使用Parent方法?
內容越具體,它在代碼中的優先級越高。
方法與父母同名的孩子優先。 您可以通過說super.method()
調用父方法。
您已經在繼承的類中聲明了一個新的構造方法,因此它將使用該方法,但是當繼承的類調用父類構造方法時,它將使用該方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.