Blogger Information
Blog 33
fans 3
comment 1
visits 23290
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的继承、属性拓展、重载(5月3日课程)2018/05/14
箭里飘香
Original
765 people have browsed it

父类

<?php


class Computer
{
    protected $brand;
    protected $model;
    protected $price;

    //构造方法
    public function __construct($brand,$model,$price)
    {
        $this->brand = $brand;
        $this->model = $model;
        $this->price = $price;
    }
    public function program()
    {
        return 'IE浏览器';
    }
}

运行实例 »

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

子类

<?php

class Lenovo extends Computer
{
    public function __get($name)
    {
        return $this->$name;
    }

    //1.对父类属性进行拓展
    private $bluetooth = false;//是否有蓝牙功能
    private $usb3 = false;//USB3.0支持
    private $handwriting = false; //手写功能

    public function __construct($brand, $model, $price, $bluetooth, $usb3, $handwriting)
    {
        parent::__construct($brand, $model, $price);

        $this->bluetooth = $bluetooth;
        $this->usb3 = $usb3;
        $this->handwriting = $handwriting;
    }

    //2.增加新方法
    public function restore()
    {
        return '支持一键还原';
    }
    //3.类方法重写/重载
    public function program()
    {
        return parent::program().'、联想电脑管家、Office';
    }

}

运行实例 »

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

实例化子类

<?php

//使用自动加载器加载类
spl_autoload_register(function ($classname){
    require './class/'.$classname.'.php';
});

$lenovo = new Lenovo('Lenovo','Win10','5499',true,true,true);

echo '品牌:'.$lenovo->brand.'<br>';
echo '系统:'.$lenovo->model.'<br>';
echo '价格:'.$lenovo->price.'元<br>';

echo '蓝牙:'.($lenovo->bluetooth ? '支持':'不支持').'<br>';
echo 'USB3.0:'.($lenovo->usb3 ? '支持':'不支持').'<br>';
echo '手写:'.($lenovo->handwriting ? '支持':'不支持').'<br>';

echo '预装软件:'.$lenovo->program().'<br>';
echo $lenovo->restore();

运行实例 »

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


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