解决此问题的最佳方法是什么?
我们有一个羊场,有两台羊毛收集机。
绵羊具有以下属性:年龄:年轻和年老。 颜色:黑白。
年龄和颜色值都是随机。
如果绵羊老了,两台机器只收羊毛。如果没有,将回应:
这只羊是 还没准备好剪毛。
我只需要一些关于syntaxis的指导即可开始使用。谢谢!
答案 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;
{
// ...
答案 1 :(得分: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(); }
答案 2 :(得分:0)
您的计划中有哪些类型的内容?绵羊和机器。
羊和机器应该是同一类吗?那么,他们是否共享相同的属性?机器有年龄吗?不。机器有颜色吗?不,那他们不是同一个班级。
制作一个羊类,给它年龄和颜色属性。类构造函数应该随机分配age和color属性的值,以便在创建每个sheep对象时设置它们。
制作机器类。它需要属性来保存黑色羊毛的数量和收集的白色羊毛的数量。
为两个类中的属性创建setter和getter。
现在编写创建绵羊和机器对象的程序,并执行所需的任务。
答案 3 :(得分: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";
?>