热搜:NVER 

关于php承袭的疑惑

2024-04-29 12:57:02
 关于php承袭的疑惑

关于php继承的疑惑

PHP code

<?php class A {
    [color=#FF0000]private[/color] $name = "Nciaer";

    public function say() {
        echo $this -> name . "
"; } } class B extends A { public $name = "Fansa"; } $b = new B(); $b -> say(); ?>

输出结果:
Nciaer
----------------------------

PHP code

<?php class A {
    [color=#FF0000]public[/color] $name = "Nciaer";

    public function say() {
        echo $this -> name . "
"; } } class B extends A { public $name = "Fansa"; } $b = new B(); $b -> say(); ?>


输出结果:
Fansa


为什么父类变量$name为public的时候,$b调用成员方法say()输出子类的$name;
当父类变量$name为private时,$b调用成员方法say()输出父类的$name;

------解决方案--------------------
private 私有的
怎么能被继承呢?