什么是类的继承?说白了,我觉得就是提高代码使用效率的。下面我就给大家介绍下继承。大理石平台维修
类的继承概念
子类继承父类的所有成员变量个方法,包括构造方法,当子类被实例化时,php会现在子类中查询构造方法,如果子类有自己的构造方法,php会先调用子类中的方法;当子类中没有时,php则去调用父类中的构造方法,这也就是我们说的继承。
类的继承是通过关键字extends,语法为:
A代表子类,B代表父类。
好了,了解了基本概念,我们就看看类的继承实例吧:
首先创建一个类,类中有不同的方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?php
class Lol{
public $name;
public $type;
public $price;
public function __construct($name,$price){
$this->name = $name;
$this->price = $price;
}
function ShowInfo(){
echo "在这不显示";
}
}
class Play extends Lol{
public $type;
public function __construct($name,$type){
$this->name = $name;
$this->type = $type;
}
function ShowInfo(){
if($this->type == "mid"){
return $this->name . "会玩这个位置";
}else{
return $this->name . "不会玩这个位置";
}
}
}
$player = new Play("faker","mid");
echo $player->ShowInfo();
|
以上就是php面向对象:类的继承实例讲解的详细内容,