【发布时间】:2011-07-15 20:04:12
【问题描述】:
我正在创建一个羊毛农场,如果羊老了,他们会在机器里剪毛。羊毛是按颜色来计算的,有黑有白的。
每个提到的属性都是随机的:
- 年龄:年轻 = 0,老 = 1
- 颜色(黑色、白色)。
逻辑我明白了,但是我不知道怎么把羊群发到机器上,而且如果我多做羊,我不能让计数器增加。 (在分配过程中,我被要求这样做)。
我对代码进行了注释以进一步理解: http://gabrielmeono.com/theFarm.zip
<?php
class Sheep{
//Atributes
const COLOR_WHITE = 'white';
const COLOR_BLACK = 'black';
const AGE_YOUNG = 0;
const AGE_OLD = 1;
private $_color;
private $_age;
//Random age and color
public static function makeRandom(){
if(rand(0, 1)){
$color = self::COLOR_WHITE;
}else{
$color = self::COLOR_BLACK;
}
$age = rand(0, 1);
return new self($color, $age);
}
public function __construct($color, $age){
$this->_color = $color;
$this->_age = $age;
}
//To check if the sheep was created and had all the atributes.
public function report(){
echo '<br>This sheep is '.$this->_color.' and '.$this->_age.'<br/>';//Old age is 1.
}
}
class machine {
//The machine should shear wool only if the sheep is old. Old equals 1.
public function Shear($collector){
switch($collector)
{
case $sheep->_age = 1;
echo 'Sheep sheared <br/>';
break;
default:
echo 'This sheep is not ready to be sheared <br/>';
break;
}
}
//Here I should be able to count and separate wool by color.
public function Counter($whiteWool, $blackWool, $count){ //This is my notion how it should start...Correct me if I'm wrong.
}
}
$sheep = Sheep::makeRandom();
$sheep -> report();
$machine = new machine ();
$machine -> Shear(0);
//I don't know how to connect the machine class with the sheep class.
//Batch creation and processing of sheep is beyond my understanding.
?>
【问题讨论】:
-
鉴于这是作业,我不想放弃太多。但是你应该将一个绵羊对象传递给剪切函数。
-
Gabriel,请咨询FAQ,以更好地了解本网站的运作方式。我们很乐意帮助您解决您自己代码中的特定编码问题(这不是)。我认为,提供概念性理解应该更多地属于你的老师的领域。
-
我认为你不需要静态函数来创建新羊,只需在函数构造中设置随机属性并使用
$sheep = new Sheep();抓取它 -
@George Cummings,对我来说这个问题似乎很好。