Php/php oop inheritance
来自菜鸟教程
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类可以使用公用的$ name和$ color属性以及Fruit类的公用__construct()和intro()方法。
Strawberry类也有自己的方法:message()。
PHP-继承和受保护的访问修饰符
在上一章中,我们了解到
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 ?>
在上面的示例中,我们看到如果我们尝试调用a
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-最终关键字
The
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 } } ?>