假设我有一个班级
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
}
}
Product类是否会使用重写的方法?或者它会使用Parent方法吗?
答案 0 :(得分:1)
越具体的东西,它在代码中获得的优先级越高。
与父母同名的儿童优先考虑。您可以通过super.method()
来说出父母方法。
答案 1 :(得分:0)
您已在继承的类中声明了一个新的构造方法,因此它将使用它,但是当继承的类调用父类构造方法时,它将使用它。