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)的形式来调用。
本文介绍了PHP中的类继承和构造方法的使用。通过一个具体的例子,展示了如何定义一个基类Animal,并从该类派生出子类Cat。重点讲解了在子类中如何调用父类的构造方法。
9053

被折叠的 条评论
为什么被折叠?



