Blogger Information
Blog 46
fans 3
comment 1
visits 33575
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
503类属性的自动加载与类继承
吃不起土的少年的博客
Original
797 people have browsed it

父类

实例

<?php
/医院
 * Created by PhpStorm.
 * User: 金超
 * Date: 2018/5/4
 * Time: 16:40
 */

class MovieList1
{
   protected $name='黑色弥撒';
    protected $date='2012/03/05';
    protected $price='$12.8';

   public function __construct($name,$date,$price)
   {
       $this->name=$name;
       $this->date=$date;
       $this->price=$price;

   }

   public  function  reward()
   {
       return'2005 英国电影学院奖 最佳特效奖';
   }
}

运行实例 »

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

子类

实例

<?php
/医院
 * Created by PhpStorm.
 * User: 金超
 * Date: 2018/5/4
 * Time: 17:14
 */

class MovieList2 extends MovieList1
{
  public function __get($name)
  {
      return $this->$name;
      // TODO: Implement __get() method.
  }
 //给父类新增一个属性
  private $charge='';



    public function __construct($name, $date, $price,$charge)
  {    //调用父类构造器初始化类属性
      parent::__construct($name, $date, $price);
      //手动增加新添加的属性
      $this->charge=$charge;
  }
  //增加新的方法
    public function BoxOffice(){
        return'$544,272,402美元';

    }
    //将父类改写
    public function reward()
    {
        //保留父类原有的方法 并追加新的操作
        return parent::reward().'MTV电影大奖  最佳动作场面奖';
        // TODO: Change the autogenerated stub
    }
}

运行实例 »

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

测试代码

实例

<?php
/医院
 * Created by PhpStorm.
 * User: 金超
 * Date: 2018/5/4
 * Time: 16:49
 */
spl_autoload_register(function($className){
    require './class/'.$className.'.php'; });
$movielist1=new MovieList1('黑色弥撒','2012/03/05','$12.8');
$movielist2=new MovieList2('后天','2010/10/21','$0.9',false);
echo'影视名:'.$movielist2->name.'<br>';
echo'上映日期:'.$movielist2->date.'<br>';
echo'是否收费:'.($movielist2->charge?'收费':'免费').'<br>';
echo'价格:'.$movielist2->price.'<br>';
echo'<hr style="color:sandybrown ">';
//子元素新的方法
echo'票房:'.$movielist2->BoxOffice().'<br>';

echo'<hr style="color:aquamarine ">';
//父元素
echo'获奖:'.$movielist1->reward().'<br>';
//子元素
echo'获奖:'.$movielist2->reward().'<br>';

运行实例 »

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


运行结果

QQ截图20180504182337.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