-1

我正在学习 OOP,在这里遇到了一点问题,即不理解代码。

这里是。

class ShopProduct {

    private $title;
    private $producerMainName;
    private $producerFirstName;
    protected $price;
    private $discount = 0;


    function __construct( $name, $firstName, $mainName, $price) {
        $this->title = $name;
        $this->producerFirstName = $firstName;
        $this->producerMainName = $mainName;
        $this->price = $price;
    }

    public function getProducer() {
        return "{$this->producerMainName} "."{$this->producerFirstName} \n ";
    }

    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 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 .= ": {$this->playLength()} minutes";
        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 .= ": {$this->numPages()} pages";
        return $base;
    }
}
class ShopProductWriter {

    private $products = array();
    public function addProduct($shopProduct){
        if(! ($shopProduct instanceof ShopProduct) ){
        die('object error');
        }
        $this->products[] = $shopProduct;
    }
    public function write($shopProduct) {   
        foreach($this->products as $shopProducts){
            $str = "{$shopProduct->getTitle()}: "."{$shopProduct->getProducer()}"." {$shopProduct->getPrice()}$ \n";
        }
        print $str;
    }
}
$product = new CDProduct('Song is the rythm','Zyxel','Beatz',50, 60.33);
$write = new ShopProductWriter();
$write->addProduct($product);
$write->write($product);

问题就在这里

class ShopProductWriter {

        private $products = array();
        public function addProduct($shopProduct){
            if(! ($shopProduct instanceof ShopProduct) ){
            die('object error');
            }
            $this->products[] = $shopProduct;
        }
        public function write($shopProduct) {   
            foreach($this->products as $shopProducts){
                $str = "{$shopProduct->getTitle()}: "."{$shopProduct->getProducer()}"." {$shopProduct->getPrice()}$ \n";
            }
            print $str;
        }
    }

如您所见,有条件 - 如果对象不是 ShopProduct 类型 - 会出错。但正如您所见,我正在创建 CDProduct 对象。

$product = new CDProduct('Song is the rythm','Zyxel','Beatz',50, 60.33);
$write = new ShopProductWriter();
$write->addProduct($product);
$write->write($product);

它应该显示错误。谁能告诉我我做错了什么?

4

2 回答 2

4

CDProduct 的对象也是 ShopProduct 类型。在类定义中:

class CDProduct extends ShopProduct {

所以它是两种类型的对象。

http://php.net/manual/en/language.oop5.inheritance.php

如果一个对象扩展了一个父对象或实现了一个接口,那么它也可以被认为是那种类型。

http://php.net/manual/en/language.operators.type.php

于 2013-08-08T19:37:50.680 回答
0

你已经得到了关于为什么 aCDProduct是 a 的反馈ShopProduct,所以我只是在你编写的 PHP 已经支持的一些代码上添加另一个提示:

    public function addProduct($shopProduct){
        if(! ($shopProduct instanceof ShopProduct) ){
        die('object error');
        }
        $this->products[] = $shopProduct;
    }

您可以使用类型提示来减少代码使其更具表现力,而不是自己进行检查:

    public function addProduct(ShopProduct $shopProduct) {
        $this->products[] = $shopProduct;
    }

我希望这对您正在学习 OOP 有所帮助。

于 2013-08-11T05:40:38.543 回答