2011-07-15 27 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"; 

?>