我正在建立一个羊毛农场,如果他们老了,羊会在机器上剪羊毛。羊毛按颜色计算,有黑色和白色。
每个提到的属性都是随机的:
我理解逻辑,但我不知道如何将羊群送到机器上,如果我生产更多的绵羊,我不能让计数器增加。 (在分配期间,我被要求这样做)。
我评论了代码以进一步理解: 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.
?>
答案 0 :(得分:2)
您可以使用依赖注入( todo:google for it )将Sheep
对象注入Machine
。
e.g。
$machine->addSheep($sheep);
addSheep
方法应该将$sheep
对象保存在类成员变量中。其他方法可能会在以后使用此变量。
答案 1 :(得分:2)
正如其他人所说,你可以通过注射将羊添加到机器中。 shear
方法实际上对我来说最有意义。
public function Shear(Sheep $sheep) {
switch ($sheep->age) {} // etc.
}
事实上,我会更进一步,让羊对剪毛作出反应。
public static function makeRandom() {
// ...
return new $age ? OldSheep($color) : YoungSheep($color);
}
甚至更好:
private static $ages = array(0 => 'Old', 1 => 'Young')
public static function makeRandom() {
// ...
$sheepclass = $ages[$age] . 'Sheep';
return new $sheepclass($color);
}
然后是时候剪毛了:
interface Sheepable {
function prepareToShear(machine $mach);
}
class OldSheep implements Sheepable {
public function prepareToShear(machine $mach) {
echo "Sheep is ready to be shorn";
$mach->shear($this);
}
}
class YoungSheep implements Sheepable {
public function prepareToShear(machine $mach) {
echo "Sheep is not ready to be shorn";
}
}
class machine {
//... etc.
public function Shear(Sheepable $sheep) {
$sheep->prepareToShear($this);
}
}
顺便说一句,寻求家庭作业的帮助往往会在这里得到负面的回应。
答案 2 :(得分:1)
您可以将$ sheep(您创建的对象)作为参数发送到机器类中的函数。
答案 3 :(得分:1)
我讨厌耕种 不要只是copypasta。仔细阅读,提出问题(但不重复),我们将能够提供帮助。
interface Animal{
const AGE_YOUNG = 0;
const AGE_OLD = 1;
const COLOR_WHITE = 'white';
const COLOR_BLACK = 'black';
public function getAge();
public function getColor();
}
interface Shearable extends Animal{
public function shear();
public function isSheared();
}
class Gorilla implements Shearable{
// ♫ seeeeeee my vest, see my vest, made of real gorilla chest ♫
}
class Sheep implements Shearable{
private $_isSheared = false;
private $_age = null;
private $_color = null;
public static function create(){
$age = rand(0, 1);
$color = rand(0, 1)
? self::COLOR_WHITE
: self::COLOR_BLACK;
return new self($age, $color);
}
public function __construct($age, $color){
$this->_age = $age;
$this->_color = $color;
}
public function shear(){
$this->_isSheared = true;
return $this;
}
public function isSheared(){
return $this->_isSheared;
}
public function getAge(){
return $this->_age;
}
public function getColor(){
return $this->_color;
}
}
class Machine{
private $_wool = array();
public function shearLine(Array $line){
$unshearable = array();
foreach($line as $animal){
if($this->shear($animal) !== true){
$unshearable[] = $animal;
}
}
return $unshearable;
}
public function shear(Shearable $animal){
if(!$animal->isSheared() && $animal->getAge() == Shearable::AGE_OLD){
$this->_wool[] = $animal->shear()->getColor();
return true;
}
return $animal;
}
public function getWool(){
return count($this->_wool);
}
public function getWoolByColor($color){
return count(array_keys($this->_wool, $color));
}
}
// build a machine
$machine = new Machine();
// brew up some sheep
$lineOfSheep = array();
for($i = 0; $i < 200; $i++){
$lineOfSheep[] = Sheep::create();
}
//make some underwear
$unshearable = $machine->shearLine($lineOfSheep);
// see how many sheep still need water and sunlight
var_dump(count($unshearable));
// see how much and of what color your underwear will be
var_dump($machine->getWool(), $machine->getWoolByColor(Shearable::COLOR_WHITE));