<?phpclass Bar { ???private $salary = 3000; ???public $lunch = 1000; ???// php中关于“可见性”的概念 ???public function test() { ???????$this->testPrivate(); ???????$this->testPublic(); ???????echo $this->salary; ???????echo $this->lunch; ???} ???// 这个被子类覆盖掉了,这也就解释了结果 ???public function testPublic() { ???????echo "Bar::testPublic\n"; ???} ???????private function testPrivate() { ???????echo "Bar::testPrivate\n"; ???}}class Foo extends Bar { ???// 无法覆盖子类的私有属性 ???private $salary = 5000; ???// 覆盖了子类的公有属性 ???public $lunch = 2000; ???// 覆盖了子类的公有方法 ???public function testPublic() { ???????echo "Foo::testPublic\n"; ???} ???????// 无法覆盖 ???private function testPrivate() { ???????echo "Foo::testPrivate\n"; ???}}$myFoo = new foo();$myFoo->test(); // Bar::testPrivate // Foo::testPublic?>
PHP中的继承
原文地址:https://www.cnblogs.com/wjf0/p/8111479.html