时间:2021-07-01 10:21:17 帮助过:0人阅读
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和继承...你就会明白很多