我很难理解为什么我们会得到这段代码的输出:
<?php
class Bar
{
public function test() {
$this->testPrivate();
$this->testPublic();
}
public function testPublic() {
echo "Bar::testPublic\n";
}
private function testPrivate() {
echo "Bar::testPrivate\n";
}
}
class Foo extends Bar
{
public function testPublic() {
echo "Foo::testPublic\n";
}
private function testPrivate() {
echo "Foo::testPrivate\n";
}
}
$myFoo = new foo();
$myFoo->test();
?>
所以Foo继承自Bar。$myfoo是Foo类的一个对象。Foo没有一个叫做test()的方法,因此它从父类Bar中继承了该方法。但为什么test()方法的结果是
Bar::testPrivate
Foo::testPublic
当子类重写父类的方法时,为什么第一个不是Foo::testPrivate呢?
非常感谢您的帮助!