Correction status:qualified
Teacher's comments:
<?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>';
点击 "运行实例" 按钮查看在线实例
运行结果