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
?>
亲自试一试 »
在上面的示例中,我们看到如果我们尝试从类外部调用 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
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
}
}
?>
亲自试一试 »