加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php实现parent调用父类的构造方法与被覆写的方法

发布时间:2020-12-13 02:24:35 所属栏目:PHP教程 来源:网络整理
导读:《:php实现parent调用父类的构造方法与被覆写的方法》要点: 本文介绍了:php实现parent调用父类的构造方法与被覆写的方法,希望对您有用。如果有疑问,可以联系我们。 本篇章节讲解php实现parent调用父类的构造办法与被覆写的办法.供大家参考研究.

《:php实现parent调用父类的构造方法与被覆写的方法》要点:
本文介绍了:php实现parent调用父类的构造方法与被覆写的方法,希望对您有用。如果有疑问,可以联系我们。

本篇章节讲解php实现parent调用父类的构造办法与被覆写的办法.分享给大家供大家参考.具体分析如下:PHP实战

覆写:被重新设计.PHP实战

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

要调用父类的办法,首先要找到一个引用类本身的途径:句柄(handle),PHP为此提供了parent关键字.
?
parent 调用父类的构造办法PHP实战

要引用一个类而不是对象的办法,可以使用 ::(两个冒号),而不是 ->.PHP实战

所以,parent::__construct() 以为着调用父类的 __construct() 办法.PHP实战

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

代码如下:
<?php
header('Content-type:text/html;charset=utf-8');
// 从这篇开始,类名首字母一律大写,规范写法
class ShopProduct{??? // 声明类
public $title; // 声明属性
public $producerMainName;
public $producerFirstName;
public $price;
function __construct($title,$firstName,$mainName,$price){
$this -> title = $title;??? // 给属性 title 赋传进来的值
$this -> producerFirstName= $firstName;
$this -> producerMainName = $mainName;
$this -> price= $price;
}
function getProducer(){??? // 声明办法
return "{$this -> producerFirstName }"."{$this -> producerMainName}";
}
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
return $base;
}
}
class CdProduct extends ShopProduct {
public $playLenth;
function __construct($title,$price,$playLenth){
parent::__construct($title,$price);
$this -> playLenth= $playLenth;
}
function getPlayLength(){
return $this -> playLength;
}
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
$base .= ":playing time - {$this->playLength} )";
return $base;
}
}
// 定义类
class BookProduct extends ShopProduct {
public $numPages;
function __construct($title,$numPages){
parent::__construct($title,$price);
$this -> numPages= $numPages;
}
function getNumberOfPages(){
return $this -> numPages;
}
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
$base .= ":page cont - {$this->numPages} )";
return $base;
}
}
?
?>

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

parent 调用父类被覆写的办法PHP实战

parent 关键字可以在任何覆写父类的办法中使用.覆写一个父类的办法时,我们并不希望删除父类的功能,而是拓展它,通过在当前对象中调用父类的办法可以达到这个目的.
  看看上面的代码,可以发现两个子类中 getSummaryLine() 办法中重复了许多代码,我们应该利用 ShopProduct 类中已经存在的功能,而不是重复开发:
PHP实战

代码如下:
// 父类:ShopProduct
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
return $base;
}
// 子类:CdProduct
function getSummaryLine(){
$base = parent::getSummaryLine();
$base .= ":playing time - {$this->playLength} )";
return $base;
}
// 子类:BookProduct
function getSummaryLine(){
$base = parent::getSummaryLine();
$base .= ":page cont - {$this->numPages} )";
return $base;
}

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

希望本文所述对大家的php程序设计有所帮助.PHP实战

《:php实现parent调用父类的构造方法与被覆写的方法》是否对您有启发,欢迎查看更多与《:php实现parent调用父类的构造方法与被覆写的方法》相关教程,学精学透。编程之家 52php.cn为您提供精彩教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读