问题描述
我刚接触PHP(通常)中的OOP,并且对继承有疑问.
I'm new to using OOP in PHP (And in general) and I had a question about inheritance.
我有以下课程:
class OCITable {
public function display() {
$this->drawHeader();
$this->drawFooter();
$this->drawBody();
}
private function drawHeader() {
...
}
private function drawFooter() {
...
}
private function drawBody() {
...
}
}
class OCITableServer extends OCITable {
private function drawBody() {
...
}
}
我想做的是否决私有函数drawBody()
.这似乎不起作用.我认为这是因为OCITableServer
对象调用display()
时,它将调用父类的display()
,而父类又调用了其drawBody()
,而不是新的drawBody()
.
What I'm trying to do is overrule the private function drawBody()
. This doesn't seem to work. I think this is because when a OCITableServer
object calls display()
, it calls the parent class's display()
, which in turn calls its drawBody()
, instead of the new drawBody()
.
在不重新定义子类中的display()
的情况下,我将如何完成我想做的事情?
How would I accomplish what I'm trying to do without redefining display()
in my sub class?
推荐答案
Protected
方法可以在子类中覆盖.私有功能不能.
Protected
methods can be overridden in subclasses. Private functions cannot.
这篇关于了解PHP中的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!