问题描述
我很难理解以下代码的输出:
class Bar
{
public function test() {
$this->testPublic();
$this->testPrivate();
}
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::testPublic
Bar::testPrivate
Class Foo 覆盖 testPublic()和 testPrivate(),并继承 test().当我调用 test()时,有一条明确的指令包含了 $ this 伪变量,因此在创建 $ myFoo 实例后,最终调用 test()函数的值将是 $ myFoo-> testPublic()和 $ myFoo-> testPrivate().第一个输出与我预期的一样,因为我覆盖了 testPublic()方法以回显 Foo :: testPublic .但是第二个输出对我来说毫无意义.如果我覆盖 testPrivate()方法,为什么它是 Bar :: testPrivate ?根据定义,父类的私有方法也不会被继承!这没有道理.为什么父方法被称为????
您的代码存在的问题是方法Bar::testPrivate
是private
,因此不能被子类覆盖.首先,我建议您阅读PHP的可见性- http://www.php.net/manual/en/language.oop5.visibility.php .在那里,您将了解到只能覆盖public
和protected
类成员方法/属性,而不能覆盖private
成员方法.
作为一个很好的例子,请尝试将Bar::testPrivate
方法的可见性更改为public或protected,而无需更改示例代码中的任何其他内容.现在尝试运行测试.怎么了?这个:
最大的问题是:为什么?".好了,您现在已使用私有Foo:testPrivate
覆盖了Bar::testPrivate
.此新的私有方法超出了Bar::test
的范围,因为私有类成员仅对其当前类可见,不父/子类!
因此,如您所见,OOP为类成员提供了一定数量的封装,如果您不花时间去理解它,可能会造成混乱.
I'm having a hard time trying to understand the output of the following code:
class Bar
{
public function test() {
$this->testPublic();
$this->testPrivate();
}
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();
Output:
Foo::testPublic
Bar::testPrivate
Class Foo overrides testPublic() and testPrivate(), and inherits test(). When I call test(), there is an explicit instruction envolving $this pseudo variable, so after I created $myFoo instance, the final calls of test() function would be $myFoo->testPublic() and $myFoo->testPrivate(). The first output is as I expected, since I overrode testPublic() method to echo Foo::testPublic. But the second output makes no sense to me. Why is it Bar::testPrivate if I overrode testPrivate() method? Also the private method from parent class wouldn't be inherited anyway, by definition! It makes no sense. Why is the parent method the one being called???
The problem with your code is that the method Bar::testPrivate
is private
, therefore it cannot be overridden by child classes. For starters, I recommend that you read up on visibility in PHP - http://www.php.net/manual/en/language.oop5.visibility.php. There you will learn that only public
and protected
class member methods/properties can be overridden, private
ones cannot.
As a good example, try changing the visibility of the Bar::testPrivate
method to either public or protected, without altering anything else in your example code. Now try and run your tests. What happens? This:
The big question is: "why?". Well, you have now overridden Bar::testPrivate
with a private Foo:testPrivate
. This new private method is out of scope for Bar::test
, because private class members are visible to their current class only, NOT the parent/child classes!
Therefore, as you can see, OOP provides a certain amount of encapsulation for class members, and it can be quite confusing if you don't take the time to understand it.
这篇关于私有方法覆盖和可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!