1 回答

TA貢獻1847條經(jīng)驗 獲得超11個贊
這里
$product1 = new ShopProductWriter("My Antonia", "Willa", "Cather", 5.99);
你重新創(chuàng)建了 ,直到那時是一個.由此,是一個對象。這沒有方法,它只有一種方法。這是你代碼中的一個拼寫錯誤,我認為你已經(jīng)多次重寫了你的代碼,一些舊的初始化離開了,你應(yīng)該已經(jīng)離開了。在這種形式下,可能做你想要的:$product1ShopProduct$product1ShopProductWritergetProducer()write()
<?php
class ShopProduct {
public $title;
public $producerSurName;
public $producerFirstName;
public $price = 0;
public function __construct (
$title,
$firstName,
$surName,
$price
)
{
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerSurName = $surName;
$this->price = $price;
}
public function getProducer () {
return $this->producerFirstName . " " . $this->producerSurName;
}
}
class ShopProductWriter {
public function write ($shopProduct){
$str=$shopProduct->title . ":" . $shopProduct->getProducer()
. " (".$shopProduct->price.")\n";
print $str;
}
}
$product1 = new ShopProduct("My Antonia", "Willa", "Cather", 5.99);
$writer = new ShopProductWriter();
$writer->write($product1);
- 1 回答
- 0 關(guān)注
- 70 瀏覽
添加回答
舉報