Home > Backend Development > PHP Tutorial > php手册当中的问题

php手册当中的问题

WBOY
Release: 2016-06-06 20:21:24
Original
1088 people have browsed it

<code class="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(); // Bar::testPrivate 
                // Foo::testPublic</code>
Copy after login
Copy after login

请大神解释下这段代码,就最后面输出为什么是这样?

回复内容:

<code class="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(); // Bar::testPrivate 
                // Foo::testPublic</code>
Copy after login
Copy after login

请大神解释下这段代码,就最后面输出为什么是这样?

private 可见度的方法不存在继承一说,所以调用方法在哪个类里,就是调用那个类对应的private方法(如果不存在就报错了)

坑爹的继承链方法调用..会把很多人绕晕..

建议看看c语音实现class和继承...你就会明白很多

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template