Blogger Information
Blog 64
fans 2
comment 3
visits 75562
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类(类的继承)—2018年5月4日09:25:32
清雨的博客
Original
600 people have browsed it

class/Computer.php

实例

<?php

/**
 * Created by PhpStorm.
 * User: 清雨
 * Date: 2018/5/4
 * Time: 8:28
 */
class Computer
{
    protected $brand;
    protected $model;
    protected $size;
    public function __construct($brand, $model,$size)
    {
        $this -> brand = $brand;
        $this -> model = $model;
        $this -> size  = $size;
    }
    public function game()
    {
        return '玩吃鸡游戏';
    }
}

运行实例 »

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

Specifications.php 继承Computer.php 

class/Specifications.php

实例

<?php

/**
 * Created by PhpStorm.
 * User: 清雨
 * Date: 2018/5/4
 * Time: 8:57
 */
class Specifications extends Computer
{
    public function __get($name)
    {
        return $this -> $name;
    }
    private $specifications;
    private $price;
    public function __construct($brand, $model, $size, $specifications, $price)
    {
        parent::__construct($brand, $model, $size);
        $this ->specifications = $specifications;
        $this ->price = $price;
    }
}

运行实例 »

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

index.php

实例

<?php
/**
 * Created by PhpStorm.
 * User: 清雨
 * Date: 2018/5/4
 * Time: 9:01
 */
spl_autoload_register(function ($className){
    require './class/'.$className.'.php';
});
$specifications = new Specifications('联想','拯救者R720','15','笔记本电脑','6099');
echo '品牌:'.$specifications ->brand.'<br>';
echo '型号:'.$specifications ->model.'<br>';
echo '尺寸:'.$specifications ->size.'<br>';
echo '价格:'.$specifications ->price.'<br>';
echo '类型:'.$specifications ->specifications.'<br>';
echo '<hr>';
echo '能玩什么游戏:'.$specifications ->game().'<br>';

运行实例 »

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

2.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