<?php class father{
function __construct(){
echo"I am a father.<br>";
}
}

class son extends  father{
function son1(){
echo"I am a son.<br>";
}
}

$peo=new son();

?>请问输出结果是什么?程序的执行步骤?非常感谢!

解决方案 »

  1.   

    输出:I am a father.初始化子类的时候,具有构造函数的类会在每次创建对象时先调用此方法原因是子类son继承了父类father的构造函数!当然你也可以在子类里写子类独有的构造函数!
      

  2.   

    首先你的这个程序只会输入I am a father.因为可能是你写错了son1不是son的构造函数,子类继承父类,对构造函数进行重写,会覆盖父类的函数内容(在本类等中),建议使用__construct()做构造函数名称,不推荐使用和类名相同的函数名作为构造函数!楼主要多多看看面向对象,努力啊!
      

  3.   


    <?php
    class A{
        function test(){
        echo "hello!";
        }
    }
    class B extends A{//若A类和B类不在同一文件中 请包含后(include)再操作
        function test2(){
            parent::test();//子类调用父类方法
            echo "test2";
        }
    }
    $a = new B();
    $a->test();//hello!
    $a->test2();//hello!test2
    ?>