2011-07-15 26 views
0

解決此問題的最佳方法是什麼?PHP:面向對象的示例(作業)

我們有一個養羊場,有兩個羊毛收集機。

  • 羊具有以下屬性:年齡:年輕和老。 顏色:黑與白。

    年齡和顏色值均爲隨機

  • 如果羊年老,兩臺機器都只收集羊毛。如果沒有迴音必:

這隻羊 沒有準備好要被剪切。

  • 的動物應該在組進行處理,在連續 實施例20羊。並應有一個專櫃共 羊毛收集在每臺機器,兩個黑色&白色。

我只是需要一些指導和syntaxis來開始。謝謝!

+0

還有我不知道如何與這些隨機值的工作,我應該之前讓他們函數啓動,還是在數組內? –

+4

做你自己的家庭作業。 – 2011-07-15 02:52:46

+0

我想我試圖 –

回答

2

羊和機器是單獨的對象。這裏有一個開始:

class Sheep{ 

    const COLOR_WHITE = 'white'; 
    const COLOR_BLACK = 'black'; 
    const AGE_YOUNG = 0; 
    const AGE_OLD = 1; 

    private $_color; 
    private $_age; 

    public static function makeRandom(){ 
     $color = rand(0, 1) 
      ? self::COLOR_WHITE 
      : self::COLOR_BLACK; 
     $age = rand(0, 1); 
     return new self($color, $age); 
    } 

    public function __construct($color, $age){ 
     $this->_color = $color; 
     $this->_age = $age; 
    } 

} 

$sheep = Sheep::makeRandom(); 

讓我們知道你在哪裏進一步。


換出三元運算符:

// ... 
if(rand(0, 1)){ 
    $color = self::COLOR_WHITE; 
}else{ 
    $color = self::COLOR_BLACK; 
{ 
// ... 
+0

看起來很清楚,除了聲明: >? self :: COLOR_WHITE >:self :: COLOR_BLACK; 爲什麼有?在第一個和a:在第二個。 –

+0

@Gabriel - 它是一個三元運算符,請參閱http://ca.php.net/ternary。你可以很容易地把它換成'if/else',我會相應地更新我的答案。基本上,如果提供的條件(在這種情況下'rand(0,1)'評估爲'true',它將返回'?'後面的值,否則':'後面的值將存儲在變量'$ color '。 – Dan

0

你的程序中有什麼類型的東西?羊和機器。

羊和機器應該是同一班嗎?那麼,他們有共同的屬性嗎?機器有年齡嗎?號機器有顏色嗎?不,然後他們不是同一班。

做一個羊類,給它的年齡和顏色屬性。類構造函數應隨機分配年齡和顏色屬性的值,以便在創建每個綿羊對象時進行設置。

製作機器類。它需要保存多少黑毛和收集多少白毛的特性。

爲這兩個類的屬性創建setter和getters。

現在編寫創建羊和機器對象的程序,並執行所需的任務。

1

嗯,這裏有幾件事情要注意,但是您知道您需要$age$color屬性以及讀取這些屬性的方法。有機會,你不希望能夠寫他們。

所以,我想可能有:

getAge(){ return $this->age; } 
getColor(){ return $this->color; } 

現在,要隨機指定的顏色和年齡,這意味着你需要的rand功能(也有其他選擇,但蘭特對你很好)。現在,如果我要這樣做,我會把這樣的東西放在構造函數中:

// assuming we're testing for male or female 
// you really should look into why this works. 
$this->gender = (rand(0, 1))? self::MALE: self::FEMALE; 
// notice the self::MALE and self::FEMALE? Those are class constants. 
// http://php.net/manual/en/language.oop5.constants.php 
// if you want to get this question *right* you'll need to look at those 

你的機器其實很簡單。他們只測試每隻羊的年齡是否足以被砍掉,然後基於這一點增加一個計數器。

// assuming they are looking for a count of female sheep 
// and state variables 'male' and 'female' which are initialized at 0 
function processSheep($sheep) 
{ 
    foreach($sheep as $test)// stupid self-pluralizing nouns. 
    { 
     if($sheep->getGender() == Sheep::MALE) $this->males++; 
     else $this->females++; // obviously, you're going to need to swap 
           // out one of these increments for an echo. 
    } 
} 

function getNumberOfMales(){ return $this->males; } 

隨着兩臺機器,來計算男性的數目:

$mach1->getNumberOfMales() + $mach2->getNumberOfMales(); 

當n機器的陣列,男子的數目:

$males = 0; 
foreach($machs as $mach){ $males += $mach->getNumberOfMales(); } 
0

擴展在TomcatExodus:

<?php 

class Sheep{ 

    const COLOR_WHITE = 'white'; 
    const COLOR_BLACK = 'black'; 
    const AGE_YOUNG = 0; 
    const AGE_OLD = 1; 

    private $_color; 
    private $_age; 
    private $_sheered; 

    public function makeRandom() { 
     $this->_color = rand(0, 1) 
      ? self::COLOR_WHITE 
      : self::COLOR_BLACK; 
     $this->_age = rand(0, 1); 
    } 

    public function __construct(){ 
     $this->makeRandom(); 
    } 

    public function sheer() { 
     if($this->_age == 0) { 
      $this->_sheered['This sheep is not ready to be sheared.'] = $this->_sheered['This sheep is not ready to be sheared.'] + 1; 
     } else { 
      if($this->_color == 'black') { 
       $this->_sheered['black'] = $this->_sheered['black'] + 1; 
      } else { 
       $this->_sheered['white'] = $this->_sheered['white'] + 1; 
      } 
     } 
    } 

    public function results() { 
     return print_r($this->_sheered,true); 
    } 
} 

$batch = 20; 
$sheep = new Sheep(); 

while($batch > 0) { 
    $sheep->makeRandom(); 
    $sheep->sheer(); 
    $batch--; 
} 
echo $sheep->results()."\n"; 

?>