php手册当中的问题
程序员文章站
2022-06-13 11:18:59
...
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(); // Bar::testPrivate
// Foo::testPublic
请大神解释下这段代码,就最后面输出为什么是这样?
回复内容:
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(); // Bar::testPrivate
// Foo::testPublic
请大神解释下这段代码,就最后面输出为什么是这样?
private 可见度的方法不存在继承一说,所以调用方法在哪个类里,就是调用那个类对应的private方法(如果不存在就报错了)
坑爹的继承链方法调用..会把很多人绕晕..
建议看看c语音实现class和继承...你就会明白很多