PHP面向对象继承用法详解(优化与减少代码重复)
本文实例讲述了php面向对象继承用法。分享给大家供大家参考,具体如下:
继承
先看两个类
<?php class cdproduct { public $playlength; // 播放时间 public $title; public $producermainname; public $producerfirstname; public $price; function __construct( $title, $firstname, $mainname, $price, $playlength ) { $this->title = $title; $this->producerfirstname = $firstname; $this->producermainname = $mainname; $this->price = $price; $this->playlength = $playlength; } function getplaylength() { return $this->playlength; } function getsummaryline() { $base = "{$this->title} ( {$this->producermainname}, "; $base .= "{$this->producerfirstname} )"; $base .= ": playing time - {$this->playlength}"; return $base; } function getproducer() { return "{$this->producerfirstname}". " {$this->producermainname}"; } } class bookproduct { public $numpages; // 看的页数 public $title; public $producermainname; public $producerfirstname; public $price; function __construct( $title, $firstname, $mainname, $price, $numpages ) { $this->title = $title; $this->producerfirstname = $firstname; $this->producermainname = $mainname; $this->price = $price; $this->numpages = $numpages; } function getnumberofpages() { return $this->numpages; } function getsummaryline() { $base = "{$this->title} ( {$this->producermainname}, "; $base .= "{$this->producerfirstname} )"; $base .= ": page count - {$this->numpages}"; return $base; } function getproducer() { return "{$this->producerfirstname}". " {$this->producermainname}"; } } $product1 = new cdproduct("cd1", "bob", "bobbleson", 4, 50 ); print $product1->getsummaryline(); print "\n"; $product2 = new bookproduct("book1", "harry", "harrelson", 4, 30 ); print $product2->getsummaryline(); print "\n"; ?>
输出:
cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30
点评:这两个类,代码重复性太高,有相同性,也有差异性。不如用继承来简化处理。
采用继承来处理
<?php class shopproduct { public $numpages; public $playlength; public $title; public $producermainname; public $producerfirstname; public $price; function __construct( $title, $firstname, $mainname, $price, $numpages=0, $playlength=0 ) { $this->title = $title; $this->producerfirstname = $firstname; $this->producermainname = $mainname; $this->price = $price; $this->numpages = $numpages; $this->playlength = $playlength; } function getproducer() { return "{$this->producerfirstname}". " {$this->producermainname}"; } function getsummaryline() { $base = "$this->title ( {$this->producermainname}, "; $base .= "{$this->producerfirstname} )"; return $base; } } class cdproduct extends shopproduct { 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 { function getnumberofpages() { return $this->numpages; } function getsummaryline() { $base = "{$this->title} ( {$this->producermainname}, "; $base .= "{$this->producerfirstname} )"; $base .= ": page count - {$this->numpages}"; return $base; } } $product1 = new cdproduct("cd1", "bob", "bobbleson", 4, null, 50 ); print $product1->getsummaryline(); print "\n"; $product2 = new bookproduct("book1", "harry", "harrelson", 4, 30 ); print $product2->getsummaryline(); print "\n"; ?>
输出:
cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30
点评:继承处理很好的解决了差异性,相通性问题。
进一步优化处理
<?php class shopproduct { // 抽离出共有属性 public $title; public $producermainname; public $producerfirstname; public $price; function __construct( $title, $firstname, $mainname, $price ) { $this->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 $playlength; function __construct( $title, $firstname, $mainname, $price, $playlength ) { parent::__construct( $title, $firstname, $mainname, $price ); // 继承父类的构造函数 $this->playlength = $playlength; // 处理自己专有的属性 } 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, $firstname, $mainname, $price, $numpages ) { parent::__construct( $title, $firstname, $mainname, $price ); $this->numpages = $numpages; } function getnumberofpages() { return $this->numpages; } function getsummaryline() { $base = "$this->title ( $this->producermainname, "; $base .= "$this->producerfirstname )"; $base .= ": page count - $this->numpages"; return $base; } } $product1 = new cdproduct("cd1", "bob", "bobbleson", 4, 50 ); print $product1->getsummaryline(); print "\n"; $product2 = new bookproduct("book1", "harry", "harrelson", 4, 30 ); print $product2->getsummaryline(); print "\n"; ?>
输出:
cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30
点评:这里把共有属性在父类中,其他个性属性放在自己的类中处理。并设置自己的构造方法,继承父类的构造方法。
进一步继承父类的方法
<?php class shopproduct { public $title; public $producermainname; public $producerfirstname; public $price; function __construct( $title, $firstname, $mainname, $price ) { $this->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 $playlength; function __construct( $title, $firstname, $mainname, $price, $playlength ) { parent::__construct( $title, $firstname, $mainname, $price ); $this->playlength = $playlength; } function getplaylength() { return $this->playlength; } function getsummaryline() { $base = parent::getsummaryline(); $base .= ": playing time - {$this->playlength}"; return $base; } } class bookproduct extends shopproduct { public $numpages; function __construct( $title, $firstname, $mainname, $price, $numpages ) { parent::__construct( $title, $firstname, $mainname, $price ); $this->numpages = $numpages; } function getnumberofpages() { return $this->numpages; } function getsummaryline() { $base = parent::getsummaryline(); $base .= ": page count - {$this->numpages}"; return $base; } } $product1 = new cdproduct("cd1", "bob", "bobbleson", 4, 50 ); print $product1->getsummaryline(); print "\n"; $product2 = new bookproduct("book1", "harry", "harrelson", 4, 30 ); print $product2->getsummaryline(); print "\n"; ?>
输出:
cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30
点评:同样的结果,可以优化优化再优化。这里继承父类的方法。parent::getsummaryline()。不过这个用的比较少。
继续添加一些有意思的内容
<?php class shopproduct { private $title; private $discount = 0; private $producermainname; private $producerfirstname; protected $price; function __construct( $title, $firstname, $mainname, $price ) { $this->title = $title; $this->producerfirstname = $firstname; $this->producermainname = $mainname; $this->price = $price; } function setdiscount( $num ) { $this->discount=$num; } function getprice() { return ($this->price - $this->discount); } function getproducer() { return "{$this->producerfirstname}". " {$this->producermainname}"; } function getsummaryline() { $base = "{$this->title} ( {$this->producermainname}, "; $base .= "{$this->producerfirstname} )"; return $base; } } class cdproduct extends shopproduct { public $playlength; function __construct( $title, $firstname, $mainname, $price, $playlength ) { parent::__construct( $title, $firstname, $mainname, $price ); $this->playlength = $playlength; } function getplaylength() { return $this->playlength; } function getsummaryline() { $base = parent::getsummaryline(); $base .= ": playing time - {$this->playlength}"; return $base; } } class bookproduct extends shopproduct { public $numpages; function __construct( $title, $firstname, $mainname, $price, $numpages ) { parent::__construct( $title, $firstname, $mainname, $price ); $this->numpages = $numpages; } function getprice() { return $this->price; } function getnumberofpages() { return $this->numpages; } function getsummaryline() { $base = parent::getsummaryline(); $base .= ": page count - {$this->numpages}"; return $base; } } $product1 = new cdproduct("cd1", "bob", "bobbleson", 4, 50 ); $product1->setdiscount( 3 ); print $product1->getsummaryline(); print "\n"; print "price: {$product1->getprice()}\n"; $product2 = new bookproduct("book1", "harry", "harrelson", 4, 30 ); $product2->setdiscount( 3 ); // 折扣对book无效 print $product2->getsummaryline(); print "\n"; print "price: {$product2->getprice()}\n"; ?>
输出:
cd1 ( bobbleson, bob ): playing time - 50
price: 1
book1 ( harrelson, harry ): page count - 30
price: 4
点评:父类添加了折扣,book继承之后,修改了getprice方法,所以折扣对book无效。
私有化属性,通过方法来设置与获取
<?php class shopproduct { // 私有化属性,通过方法来设置与获取 private $title; private $producermainname; private $producerfirstname; protected $price; private $discount = 0; public function __construct( $title, $firstname, $mainname, $price ) { $this->title = $title; $this->producerfirstname = $firstname; $this->producermainname = $mainname; $this->price = $price; } public function getproducerfirstname() { return $this->producerfirstname; } public function getproducermainname() { return $this->producermainname; } public function setdiscount( $num ) { $this->discount=$num; } public function getdiscount() { return $this->discount; } public function gettitle() { return $this->title; } public function getprice() { return ($this->price - $this->discount); } public function getproducer() { return "{$this->producerfirstname}". " {$this->producermainname}"; } public function getsummaryline() { $base = "{$this->title} ( {$this->producermainname}, "; $base .= "{$this->producerfirstname} )"; return $base; } } class cdproduct extends shopproduct { private $playlength = 0; public function __construct( $title, $firstname, $mainname, $price, $playlength ) { parent::__construct( $title, $firstname, $mainname, $price ); $this->playlength = $playlength; } public function getplaylength() { return $this->playlength; } public function getsummaryline() { $base = parent::getsummaryline(); $base .= ": playing time - {$this->playlength}"; return $base; } } class bookproduct extends shopproduct { private $numpages = 0; public function __construct( $title, $firstname, $mainname, $price, $numpages ) { parent::__construct( $title, $firstname, $mainname, $price ); $this->numpages = $numpages; } public function getnumberofpages() { return $this->numpages; } public function getsummaryline() { $base = parent::getsummaryline(); $base .= ": page count - {$this->numpages}"; return $base; } public function getprice() { return $this->price; } } $product1 = new cdproduct("cd1", "bob", "bobbleson", 4, 50 ); print $product1->getsummaryline()."\n"; $product2 = new bookproduct("book1", "harry", "harrelson", 4, 30 ); print $product2->getsummaryline()."\n"; ?>
输出:
cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30
点评:这里进一步私有化了属性,要想获取只能通过方法。这样就确保了安全性。
更多关于php相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。