PHP OOP - 继承
PHP - 什么是继承?
OOP 中的继承 = 一个类派生于另一个类。
子类将继承父类的所有公共和受保护的属性和方法。此外,它还可以拥有自己的属性和方法。
通过使用 extends
关键字来定义一个继承的类。
我们来看一个例子
示例
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
自己动手试一试 »
示例解释
Strawberry 类继承自 Fruit 类。
这意味着 Strawberry 类可以因为继承而使用 Fruit 类的公共 $name 和 $color 属性以及公共 __construct() 和 intro() 方法。
Strawberry 类还有自己的方法:message()。
PHP - 继承和 Protected 访问修饰符
在前一章中,我们了解到 protected
属性或方法可以在类内部以及从该类派生的类中访问。这是什么意思?
我们来看一个例子
示例
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
$strawberry->message(); // OK. message() is public
$strawberry->intro(); // ERROR. intro() is protected
?>
自己动手试一试 »
在上面的示例中,我们看到如果我们尝试从类外部调用一个 protected
方法(intro()),我们会收到一个错误。public
方法可以正常工作!
我们来看另一个例子
示例
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
// Call protected method from within derived class - OK
$this -> intro();
}
}
$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
$strawberry->message(); // OK. message() is public and it calls intro() (which is protected) from within the derived class
?>
自己动手试一试 »
在上面的示例中,我们看到一切正常!这是因为我们从派生类内部调用了 protected
方法(intro())。
PHP - 重写继承的方法
子类通过重新定义方法(使用相同的方法名)来覆盖继承的方法。
请看下面的示例。子类 (Strawberry) 中的 __construct() 和 intro() 方法将覆盖父类 (Fruit) 中的 __construct() 和 intro() 方法。
示例
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public $weight;
public function __construct($name, $color, $weight) {
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
}
public function intro() {
echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
}
}
$strawberry = new Strawberry("Strawberry", "red", 50);
$strawberry->intro();
?>
自己动手试一试 »
PHP - final 关键字
final
关键字可用于阻止类继承或阻止方法覆盖。
下面的示例说明了如何阻止类继承
下面的示例说明了如何阻止方法覆盖
示例
<?php
class Fruit {
final public function intro() {
// 一些代码
}
}
class Strawberry extends Fruit {
// 将导致错误
public function intro() {
// 一些代码
}
}
?>
自己动手试一试 »