在過去的幾個月中,我學習純粹的操作系統已經走過了很長的路,現在我正在將設計模式應用到我的工作中!所以我不得不擴展我的php知識,並且我正在使用接口,擴展它們,然後實現這些接口的類。我的問題是有關從延伸另一個藉此代碼實例的接口構造類:面向對象的PHP與關鍵字接口,擴展,實現
interface Car
{
function doGeneralCarStuff();
vinNumber =;
}
interface CompactCar extends Car
{
static $numCompactCars;
function doCompactCarStuff();
}
class HondaCivic implements CompactCar
{
function doGeneralCarStuff()
{
//honk horn , blink blinkers, wipers on, yadda yadda
}
function doCompactCarStuff()
{
//foo and bar and foobar
}
}
class ToyotaCorolla implements CompactCar
{
function doGeneralCarStuff()
{
//honk horn , blink blinkers, wipers on, yadda yadda
}
function doCompactCarStuff()
{
//foo and bar and foobar
}
}
myCar = new HondaCivic();
myFriendCar = new ToyotaCorolla();
好了,現在可以說,我想知道的東西從我的本田延伸即CompactCar接口接口的約一。我想知道有多少小型車($ numCompactCars)已經創建。我是新來的深層(對我來說很深:p)OOP,所以如果我沒有正確地做到這一點,請提供指導。非常感謝!
如此。但我正在嘗試使用「更多」面向對象的方法來實現它。我想從層次結構中獲取信息。不管怎麼說,還是要謝謝你! – 2011-03-18 06:04:40
啊,嘗試工廠設計模式。它是一個創建其他對象的類(即$ carFactory-> createCar('HondaCivic');)。在該功能中,您可以將汽車存儲在類似上述的屬性(類變量)中,並且有一種名爲「countCompacts」的方法,該方法循環已經創建的汽車並返回總數。現在這是一個面向對象的方法。 – RDL 2011-03-18 06:12:19
謝謝!我幾乎處於我書中的工廠設計模式。 – 2011-03-18 06:18:41