2012-03-28 6 views
0

私はOOPを新しくしていますので、厳しくしないでください。これはなぜ機能しないのですか? OOPの初心者

"div style="background-color:RGB(127,0,0);width:100px;height:50px"></div>" 

以下は私のOOPコードです:

$color = new Color(127,0,0); 
$rect = new Rectangle($color, 100, 50); 
$rect->render(); 

ページに次のコードを持参してください:

私の仕事は、これがあることです。目的は、抽象クラスComponentと抽象メソッドrender()を使用することでした。私は、コードが動作しない理由を把握しようとしています:

class Color { 
    protected $red; 
    protected $green; 
    protected $blue; 
    public function __construct($red, $green, $blue) { 
    $this->red = $red; 
    $this->green = $green; 
    $this->blue = $blue; 
    } 
} 
    abstract class Component { 

    protected $color; 
    protected $width; 
    protected $height; 

    public function __construct($color) { 

    $this->color = new Color(); 

    } 

    abstract function render(); 

    } 
    class Rectangle extends Component { 
    public function __construct($color, $width, $height){ 
    parent::__construct(); 
    $this->color = $color; 
    $this->width = $width; 
    $this->height = $height; 
    } 
    public function render() { 
    echo "<div style='background-color:RGB(" . $this->color . ");width:" . $this->width .  "px;height:" . $this->height . "px'></div>"; 
    } 
    } 
    $color = new Color(127,0,0); 
    $rect = new Rectangle($color, 100, 50); 
    echo $rect->render(); 
+0

間違っています! )*あなたが*なぜ*になる前に何が働いていないのですか?通常、最初のものは直接2番目のものにつながります。 –

+0

何が失敗していますか?エラーメッセージが表示されていますか?ページが完全に読み込めませんか? –

+0

警告:Component :: __ construct()の引数1が見つからない、26行目/public_html/index.phpで呼び出され、18行目の/public_html/index.phpに定義されています – Sorrybyenglish

答えて

1

あなたが$this->colorをエコーし​​たい場合は、Colorクラスの__toStringメソッドを定義する必要があります。

class Color { 
    protected $red; 
    protected $green; 
    protected $blue; 
    public function __construct($red, $green, $blue) { 
     $this->red = $red; 
     $this->green = $green; 
     $this->blue = $blue; 
    } 

    public function __toString() { 
     return "$this->red, $this->green, $this->blue"; 
    } 
} 

そして、あなたのコードにタイプミスがあり注意、withwidthでなければなりません。

また、

parent::__construct(); 
$this->color = $color; 

以下Rectangle::__constructメソッドのコードは

parent::__construct($color); 

する必要があり、Componentクラスは、(__constructの変化に注意してください)のようになります。

abstract class Component { 

    protected $color; 
    protected $width; 
    protected $height; 

    public function __construct($color) { 

     $this->color = $color; 

    } 

    abstract function render(); 

    } 
+0

ありがとう。これは私が欲しいものです。私は理解されましたが、ありがとうございます)) – Sorrybyenglish

+0

私は幅をしました。 – Sorrybyenglish

+0

@Sorrybyenglish '"); with: "'、あなたは 'width'を望んでいますよね? – xdazz

2

あなたは$colorオブジェクトを親クラスに渡していないか、そしてwidthの綴りはあなたが私たちに教えて(と、自分自身を理解する必要が

public function __construct($color, $width, $height){ 
    parent::__construct($color); //The parent also needs a $color as it is defined 
    $this->color = $color; 
    $this->width = $width; 
    $this->height = $height; 
} 
+0

私は知らなかった))ありがとう... – Sorrybyenglish

+0

私は幅をしました。 – Sorrybyenglish

+0

@Sorrybyenglish、機能の至るところで、幅のスペルを確認してください。アップデートを見る – Starx

関連する問題