【问题标题】:Object dependencies, member variables (homework example)对象依赖、成员变量(作业示例)
【发布时间】: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,对我来说这个问题似乎很好。

标签: php class function


【解决方案1】:

正如其他人所说,您可以通过注射将羊添加到机器中。 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);
   }
}

顺便说一句,在这里寻求家庭作业的帮助往往会得到负面的回应。

【讨论】:

  • YoungSheep 不要化蛹,从它们的茧中以OldSheep 的身份出现:)
  • 我只是想知道,为什么有一个静态工厂函数比在构造函数中设置属性并使用new Sheep(); 随机获得一只羊更好?
  • 最好有一个工厂,因为你可以创建不同的对象。如果你只能创建一种类型的对象,当然使用new ... 就可以了。
  • 谢谢,我使用了你的例子,但是我得到:可捕获的致命错误:参数 1 传递给 machine::Shear() 必须实现接口 Sheepable,没有给出,在第 64 行的 theFarm0.php 中调用并在第 56 行的 theFarm0.php 中定义这是编辑后的 ​​php 并进行了更正:gabrielmeono.com/theFarm0.zip
  • 很简单,您需要将 Sheep 对象传递给机器类。 ($machine-&gt;Shear($sheep))。确保您了解代码在此处实际执行的操作。
【解决方案2】:

您可以使用依赖注入将Sheep 对象注入Machinetodo: google for it)。

例如

$machine->addSheep($sheep);

addSheep 方法应该将$sheep 对象保存在类成员变量中。以后其他方法可能会用到这个变量。

【讨论】:

    【解决方案3】:

    您可以将 $sheep(您创建的对象)作为参数发送给机器类中的函数。

    【讨论】:

      【解决方案4】:

      我讨厌农业。
      不要只是复制意大利面。通读它,提出问题(但不能重复),我们将能够提供帮助。

      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));
      

      【讨论】:

      • ♫ ...看看这件毛衣,没有比正宗的爱尔兰塞特犬更好的了;看看这顶帽子,那是我的猫,晚装,吸血蝙蝠;这些白色拖鞋是白化病、非洲濒危犀牛♫
      • 喜欢辛普森一家的参考资料,这是一个很好的实现,类似于我介绍的那个。唯一的缺点是由于shear() 中的 if 语句而缺乏多态性。你也是在编isSheared()之类的条件,或者我没看错,但这是明智的。
      • 谢谢@tandu(你不能把作业认真)无论如何,虽然是真的,但这个实现并没有体现多态性和其他 OOP纯度,它是预期系统的有效沟通。我将isSheared() 移动到Shearable 接口以强制在类中实现。将这些属性保留为对象的状态似乎是合适的,以便轻松过渡到更复杂的系统。例如,为了获得额外的功劳,我们可以很容易地引入细粒度的“剪切”条件,或数值(而不是本质上的布尔值)老化。
      猜你喜欢
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 2021-02-28
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多