羊とマシンが別のオブジェクトである2台のマシンと
// 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; }
は、男性の数を計算します。ここにはスタートがあります:
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();
あなたが今後どこにいるのかをお知らせください。
は、三項演算子スワップアウト:TomcatExodusに拡大
// ...
if(rand(0, 1)){
$color = self::COLOR_WHITE;
}else{
$color = self::COLOR_BLACK;
{
// ...
出典
2011-07-15 03:16:05
Dan
私はそれらのランダムな値で動作するかどうかはわかりませんが、私は前にそれらを作る必要があります関数が起動するか、または配列の内部にありますか? –
あなた自身の宿題を行います。 –
あなたは宿題を宿題としてタグ付けし、あなたの質問に書いておけば、 –