Blogger Information
Blog 38
fans 0
comment 1
visits 30701
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5月3,父类与子类
1
Original
797 people have browsed it

实例

这是父类:

<?php

class Car
{
    //受保护的类
    protected $brand;//品牌
    protected $price;//价格

    //构造方法
    public function __construct($brand,$price)
    {
        $this->brand = $brand;
        $this->price = $price;
    }
    //speed方法
    public function speed()
    {
        return '我是父类';
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

这是子类:

实例

<?php

class SmartCar extends Car
{
    //私有属性
    private $autopilot = false;

    //构造方法
    public function __construct($brand, $price,$autopilot)
    {
        //父类的构造方法
        parent::__construct($brand, $price);
        $this->autopilot = $autopilot;
    }
    //魔术方法
    public function __get($name)
    {
        return $this->$name;
    }
    //导航方法
    public function navigation()
    {
        return '我是子类的方法';
    }
    //父类的方法重写
    public function speed()
    {
        return parent::speed().'+这是父类的重写';
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

这是测试脚本:

实例

<meta charset="UTF-8">
<?php
spl_autoload_register(function ($classname){
    require './class/'.$classname.'.php';
});

$smartcar = new SmartCar('Benz','1000万',false);

echo $smartcar->brand;
echo $smartcar->price;
echo ($smartcar->autopilot?'支持':'不支持');

echo $smartcar->speed();
echo $smartcar->navigation();

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
  • 1
    2018-03-16 00:39:40