21xrx.com
2024-12-22 22:28:38 Sunday
登录
文章检索 我的文章 写文章
PHP OOP - 继承
2021-07-22 19:13:08 深夜i     --     --
P H P O P -


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 - 继承和受保护的访问修饰符

在上一章中,我们了解到受保护的属性或方法可以 进入 class和由该class派生的class。 这意味着什么?

让我们来看看一个例子:

<?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
?>

在上面的例子中,我们看到,如果我们试图调用受保护的 来自类外部的方法(intro()),我们将收到错误。 message方法将正常工作!

让我们看看另一个例子:

<?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
?>

在上面的例子中,我们看到所有工作都很好! 这是因为我们称之为来自派生类中的方法(intro())。


PHP - 覆盖继承方法

通过重新定义方法可以覆盖继承的方法(使用相同的方法 名称)在子类中。

看下面的例子。 子类中的__construct()和intro()方法 类(Strawberry)将覆盖__construct()和intro()方法 父类(Fruit):

<?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
  }
}
?>


 

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复