xajh32y 发表于 2017-4-3 12:27:22

Php继承与父类构造方法的调用

  Animal类

               class Animal {
private $kind ;
function __construct($kind) {
$this -> kind = $kind;
}
function wow() {
echo $this -> kind;
}
}
  Cat类

               class Cat extends Animal {
function __construct() {
parent::__construct("Cat");
}
function mow() {
echo "mow!";
}
}
  test.php

                $cat = new Cat();
$cat -> wow();
$cat -> mow();
  可以看出php继承使用的也是extends 关键字
  php中调用父类的构造方法不是使用super关键字 , 而是使用parent::__construct(参数1 , 参数2 , 参数3)的形式来调用。
页: [1]
查看完整版本: Php继承与父类构造方法的调用