PHP面向对象学习源码备份(二)——调用父类构造函数

来源:互联网 发布:淘宝书店代理 编辑:程序博客网 时间:2024/05/16 09:52
<?php  //基类  class Animal  {   public $name; public function __construct( $name )  {  $this->name = $name;  }  }  //派生类  class Person extends Animal     {  public $personSex;   public $personAge;  function __construct( $personSex, $personAge )  {  parent::__construct( "heiyeluren" ); //调用父类构造函数$this->personSex = $personSex;  $this->personAge = $personAge;  }  function printPerson()  {  print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );  }  }  //实例化Person对象  $personObject = new Person( "male", "21");  //执行打印  $personObject->printPerson();   //输出:heiyeluren is male,this year 21  ?>  

原创粉丝点击