-1

おっと勉強中です。私はおっとをカバーし、おっとの背後にある考え方を理解しようとしています。私はコードを持っており、理解のために静的保護を使用したいと考えています。属性を宣言します: protected static $formula. そして、私は protected static $formulabyを呼び出しますself :: $formula = $this->width * $this->height;。デバッガーでコードを実行すると、$formula = null. `$formula' は = 10000 である必要があります。理由はわかりません。助けてくれてありがとう。これが私のコードです:

<?php
Class Rectangle {

//Declare the attributes:
public $width = 0;
public $height = 0;
protected static $formula = 0;

//Method to set the dimensions.
Function set_size($w = 0, $h = 0) {
        $this->width = $w;
        $this->height = $h;
        self :: $formula = $this->width * $this->height;
}

//Method to calculate and return the area.
function get_area() {
            $this->set_size(100,100);
    return ($formula);
    }

}

$rect = new Rectangle ();
echo $rect->get_area();

?>

4

1 に答える 1

4

あなたのコードには小さなバグがありget_areaます:

return ($formula);

次のようにする必要があります。

return self::$formula;
于 2012-05-03T07:07:29.770 に答える