zoukankan      html  css  js  c++  java
  • php面向对象:类的继承实例讲解

    什么是类的继承?说白了,我觉得就是提高代码使用效率的。下面我就给大家介绍下继承。大理石平台维修

    类的继承概念

    子类继承父类的所有成员变量个方法,包括构造方法,当子类被实例化时,php会现在子类中查询构造方法,如果子类有自己的构造方法,php会先调用子类中的方法;当子类中没有时,php则去调用父类中的构造方法,这也就是我们说的继承。

    类的继承是通过关键字extends,语法为:

    1

    2

    3

    class A extends B{

    ...

    }

    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  "在这不显示";

    }

    }

    //子类Play

    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面向对象:类的继承实例讲解的详细内容,

  • 相关阅读:
    [2019南昌邀请赛网络赛D][dp]
    [ Educational Codeforces Round 65 (Rated for Div. 2)][二分]
    [hdoj5927][dfs]
    [cf1140D. Minimum Triangulation][dp]
    [hdoj6483][莫队+线段树/ST]
    使用GAC加速 解决CSP问题 Kakuro
    UVA 11427 Expect the Expected
    UVA 11021 Tribles
    UVA 11174 Stand in a Line 树上计数
    《算法概论》第八章的一些课后题目 关于NP-Complete Problem
  • 原文地址:https://www.cnblogs.com/furuihua/p/12083805.html
Copyright © 2011-2022 走看看