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 类中的 public $name 和 $color 属性以及 public __construct() 和 intro() 方法。
Strawberry 类也有自己的方法:message()。
在上一章中我们了解到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())。
可以通过在子类中重新定义方法(使用相同的名称)来覆盖继承的方法。
看下面的例子。子类(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();
?>
亲自试一试 »
这个final
关键字可用于防止类继承或防止方法重写。
以下示例显示如何防止类继承:
<?php
final class Fruit {
// some code
}
// will result in error
class Strawberry extends Fruit {
// some code
}
?>
亲自试一试 »
以下示例展示了如何防止方法重写:
<?php
class Fruit {
final public function intro() {
// some code
}
}
class Strawberry extends Fruit {
// will result in error
public function intro() {
// some code
}
}
?>
亲自试一试 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!