Blogger Information
Blog 42
fans 0
comment 1
visits 25994
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
父类与子类学习-2018年5月6日上午7点20分补课完成
邵军-山东-84918的博客
Original
525 people have browsed it

父类代码:

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/6 0006
 * Time: 上午 6:42
 */

class Tv
{
protected $brand;
protected $place;
protected $price;
public function __construct($brand,$place,$price)
{
    $this->brand=$brand;
    $this->place=$place;
    $this->price=$price;
}
public function feature(){
    return '我的特点是能看有线电视';
}
}

运行实例 »

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

子类代码:

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/6 0006
 * Time: 上午 6:48
 */

class Smart_tv extends Tv
{
protected $func;
protected $repair;
//创建查询器
public function __get($name)
{
   return $this->$name;
}
//扩展功能
public function __construct($brand, $place, $price,$func,$repair)
{
    parent::__construct($brand, $place, $price);
    $this->func=$func;
    $this->repair=$repair;
}
//调用父类并改写
public function feature()
{
    return parent::feature().',还能上网看视频。';
}
}

运行实例 »

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

前台页面:

实例

<?php
//使用自动加载器
spl_autoload_register(function($className){
    require $className.'.php';
});
$Smart_tv=new Smart_tv('三星','韩国','5000','无线连接','三年');
echo '品牌:'.$Smart_tv->brand.'<br>';
echo '产地:'.$Smart_tv->place.'<br>';
echo '价格:'.$Smart_tv->price.'<br>';
echo '功能:'.$Smart_tv->func.'<br>';
echo '保修:'.$Smart_tv->repair.'<br>';
//输出父类调用改写语句
echo $Smart_tv->feature();

运行实例 »

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


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