本文共 12338 字,大约阅读时间需要 41 分钟。
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 - 50book1 ( harrelson, harry ): page count - 30
点评:这两个类,代码重复性太高,有相同性,也有差异性。不如用继承来简化处理。
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 - 50book1 ( harrelson, harry ): page count - 30
点评:继承处理很好的解决了差异性,相通性问题。
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 - 50book1 ( harrelson, harry ): page count - 30
点评:这里把共有属性在父类中,其他个性属性放在自己的类中处理。并设置自己的构造方法,继承父类的构造方法。
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 - 50book1 ( harrelson, harry ): page count - 30
点评:同样的结果,可以优化优化再优化。这里继承父类的方法。parent::getSummaryLine()。不过这个用的比较少。
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 - 50price: 1book1 ( harrelson, harry ): page count - 30price: 4
点评:父类添加了折扣,book继承之后,修改了getPrice方法,所以折扣对book无效。
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 - 50book1 ( harrelson, harry ): page count - 30
点评:这里进一步私有化了属性,要想获取只能通过方法。这样就确保了安全性。
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5169350.html,如需转载请自行联系原作者