【问题标题】:PHP: Object oriented example (Homework)PHP:面向对象的示例(家庭作业)
【发布时间】:2011-07-15 02:50:57
【问题描述】:

解决这个问题的最佳方法是什么?

我们有一个养羊场,有两台羊毛收集机。

  • 绵羊具有以下属性: 年龄:年轻和年老。 颜色:黑白。

    年龄和颜色值都是随机的

  • 两台机器都只在羊老了的情况下收集羊毛。如果不是会回显:

这只羊是 还没准备好被剪掉。

  • 应分组处理动物,以便 例如连续 20 只羊。并且应该有一个计数器的总数 每台机器收集的羊毛,黑色和白色。

我只需要一些有关语法的指导即可开始使用。谢谢!

【问题讨论】:

  • 另外我不确定如何处理这些随机值,我应该在函数启动之前创建它们还是在数组中创建它们?
  • 做你自己的家庭作业。
  • 如果您正在做作业,请将其标记为作业并在您的问题中说明。
  • 好的,谢谢提示
  • 除了作业符号,您实际上需要用面向对象的编程逻辑来解决这个问题吗?这个问题还不够详细

标签: php class object


【解决方案1】:

绵羊和机器是分开的对象。这是一个开始:

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;
{
// ...

【讨论】:

  • 对我来说看起来很清楚,除了声明:>?自我::COLOR_WHITE >: 自我::COLOR_BLACK;为什么有?在第一个和一个 : 在第二个。
  • @Gabriel - 它是一个三元运算符,请参阅ca.php.net/ternary。您可以轻松地将其换成if/else,我会相应地更新我的答案。基本上如果提供的条件(在这种情况下rand(0, 1) 评估为true 它返回? 之后的值,否则返回':' 之后的值,在这种情况下将其存储在变量$color 中。
【解决方案2】:

嗯,这里有一些事情要做,但您知道您需要$age$color 属性,以及读取这些属性的方法。不过,您可能不想编写它们。

所以,我可能会:

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

现在,您想随机分配颜色和年龄,这意味着您需要 rand 函数(那里还有其他选项,但 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(); }

【讨论】:

    【解决方案3】:

    你的程序中有哪些类型的东西?羊和机器。

    羊和机器应该是同一类吗?那么,它们是否具有相同的属性?机器有年龄吗?不,机器有颜色吗?不,那他们就不是同一个班级了。

    创建一个羊类,给它年龄和颜色属性。类构造函数应随机分配年龄和颜色属性的值,以便在创建每个羊对象时设置它们。

    创建一个机器类。它需要属性来保存它收集了多少黑羊毛和多少白羊毛。

    为两个类中的属性创建 setter 和 getter。

    现在编写创建羊和机器对象的程序,并执行所需的任务。

    【讨论】:

      【解决方案4】:

      在 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";
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-20
        • 2019-04-14
        • 2014-12-21
        • 1970-01-01
        • 2012-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多