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() {
// 一些程式碼
}
}
?>
自己動手試一試 »