Blogger Information
Blog 56
fans 3
comment 1
visits 50685
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的继承与方法重载——2018年5月4日
沈斌的博客
Original
749 people have browsed it

php类的继承与方法重载

Car.php

实例

<?php
/医院
 *
 */

class Car
{
    protected $price;
    protected $speed;

    public function __construct($price,$speed)
    {
        $this->price=$price;
        $this->speed=$speed;
    }

    public function drive (){
        return 'drive the car';
    }

}

运行实例 »

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

Jeep.php

实例

<?php
/医院
 *
 */

class Jeep extends Car
{
    private $level;
    private $sales;

    public function __construct($price, $speed,$level,$sales)
    {
        parent::__construct($price, $speed);

        $this->level=$level;
        $this->sales=$sales;

    }

    public function drive()
    {
        return parent::drive().' jeep';
    }

    public function __get($name)
    {
       return $this->$name;
    }


}

运行实例 »

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

test.php


实例

<?php
/医院
 *
 */
spl_autoload_register(function ($className){
   require './'.$className.'.php';
});

$jeep=new Jeep(200000,100,3,20000);

echo 'price:'.$jeep->price,'<br>';
echo 'speed:'.$jeep->speed,'<br>';
echo 'level:'.$jeep->level,'<br>';
echo 'sales:'.$jeep->sales,'<br>';

echo $jeep->drive();

运行实例 »

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


class.png

Correction status:qualified

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