VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php实现parent调用父类的构造方法与被覆写的方法

这篇文章主要介绍了php实现parent调用父类的构造方法与被覆写的方法,在上一篇关于使用类继承解决代码重复问题的基础上,进一步分析了parent调用父类的构造方法与被覆写的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php实现parent调用父类的构造方法与被覆写的方法。分享给大家供大家参考。具体分析如下:

覆写:被重新设计。

在子类中定义构造方法时,需要传递参数给父类的构造方法,否则我们得到的可能是一个构造不完整的对象。

要调用父类的方法,首先要找到一个引用类本身的途径:句柄(handle),PHP为此提供了parent关键字。

parent 调用父类的构造方法

要引用一个类而不是对象的方法,可以使用 ::(两个冒号),而不是 ->。

所以, parent::__construct() 以为着调用父类的 __construct() 方法。

修改上篇《使用类继承解决代码重复等问题》中的代码,让每个类只处理自己的数据:

  1. <?php 
  2. header('Content-type:text/html;charset=utf-8'); 
  3. // 从这篇开始,类名首字母一律大写,规范写法 
  4. class ShopProduct{    // 声明类 
  5. public $title; // 声明属性 
  6. public $producerMainName; 
  7. public $producerFirstName; 
  8. public $price; 
  9. function __construct($title,$firstName,$mainName,$price){ 
  10. $this -> title = $title;    // 给属性 title 赋传进来的值 
  11. $this -> producerFirstName= $firstName; 
  12. $this -> producerMainName = $mainName; 
  13. $this -> price= $price; 
  14. } 
  15. function getProducer(){    // 声明方法 
  16. return "{$this -> producerFirstName }"."{$this -> producerMainName}"; 
  17. } 
  18. function getSummaryLine(){ 
  19. $base = "{$this->title}( {$this->producerMainName},"; 
  20. $base .= "{$this->producerFirstName} )"; 
  21. return $base; 
  22. } 
  23. } 
  24. class CdProduct extends ShopProduct { 
  25. public $playLenth; 
  26. function __construct($title,$firstName,$mainName,$price,$playLenth){ 
  27. parent::__construct($title,$firstName,$mainName,$price); 
  28. $this -> playLenth= $playLenth; 
  29. } 
  30. function getPlayLength(){ 
  31. return $this -> playLength; 
  32. } 
  33. function getSummaryLine(){ 
  34. $base = "{$this->title}( {$this->producerMainName},"; 
  35. $base .= "{$this->producerFirstName} )"; 
  36. $base .= ":playing time - {$this->playLength} )"; 
  37. return $base; 
  38. } 
  39. } 
  40. // 定义类 
  41. class BookProduct extends ShopProduct { 
  42. public $numPages; 
  43. function __construct($title,$firstName,$mainName,$price,$numPages){ 
  44. parent::__construct($title,$firstName,$mainName,$price); 
  45. $this -> numPages= $numPages; 
  46. } 
  47. function getNumberOfPages(){ 
  48. return $this -> numPages; 
  49. } 
  50. function getSummaryLine(){ 
  51. $base = "{$this->title}( {$this->producerMainName},"; 
  52. $base .= "{$this->producerFirstName} )"; 
  53. $base .= ":page cont - {$this->numPages} )"; 
  54. return $base; 
  55. } 
  56. } 
  57.  
  58. ?> 

每个子类都会在设置自己的属性前调用父类的构造方法。基类(父类)现在仅知道自己的数据,而我们也应该尽量避免告诉父类任何关于子类的信息,这是一条经验规则,大家想想如果某个子类的信息应该是”保密“的,结果父类知道它的信息,其它子类可以继承,这样子类的信息就不保密了。

parent 调用父类被覆写的方法

parent 关键字可以在任何覆写父类的方法中使用。覆写一个父类的方法时,我们并不希望删除父类的功能,而是拓展它,通过在当前对象中调用父类的方法可以达到这个目的。

看看上面的代码,可以发现两个子类中 getSummaryLine() 方法中重复了许多代码,我们应该利用 ShopProduct 类中已经存在的功能,而不是重复开发:

  1. // 父类:ShopProduct 
  2. function getSummaryLine(){ 
  3. $base = "{$this->title}( {$this->producerMainName},"; 
  4. $base .= "{$this->producerFirstName} )"; 
  5. return $base; 
  6. } 
  7. // 子类:CdProduct 
  8. function getSummaryLine(){ 
  9. $base = parent::getSummaryLine(); 
  10. $base .= ":playing time - {$this->playLength} )"; 
  11. return $base; 
  12. } 
  13. // 子类:BookProduct 
  14. function getSummaryLine(){ 
  15. $base = parent::getSummaryLine(); 
  16. $base .= ":page cont - {$this->numPages} )"; 
  17. return $base; 
  18. } 

我们在父类 ShopProduct 中为 getSummaryLine() 方法完成了”核心“功能,接着在子类中简单的调用父类的方法,然后增加更多数据到摘要字符串,方法的拓展就实现了。

出处:http://www.phpfensi.com/php/20210512/15229.html


相关教程