Php 为什么';这不管用吗?OOP新手
我是OOP新手,所以请不要太苛刻 我的任务是:Php 为什么';这不管用吗?OOP新手,php,oop,Php,Oop,我是OOP新手,所以请不要太苛刻 我的任务是: $color = new Color(127,0,0); $rect = new Rectangle($color, 100, 50); $rect->render(); 应将以下代码带到页面: "div style="background-color:RGB(127,0,0);width:100px;height:50px"></div>" 如果要回显$this->color,应该为color类定义方法 cl
$color = new Color(127,0,0);
$rect = new Rectangle($color, 100, 50);
$rect->render();
应将以下代码带到页面:
"div style="background-color:RGB(127,0,0);width:100px;height:50px"></div>"
如果要回显
$this->color
,应该为color
类定义方法
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";
}
}
请注意,您的代码中有一个输入错误,with
应该是width
另外,下面的矩形中的代码::\u构造方法
parent::__construct();
$this->color = $color;
应该是
parent::__construct($color);
而组件
类应该是(注意_构造的变化):
如果要回显$this->color
,应该为color
类定义方法
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";
}
}
请注意,您的代码中有一个输入错误,with
应该是width
另外,下面的矩形中的代码::\u构造方法
parent::__construct();
$this->color = $color;
应该是
parent::__construct($color);
而组件
类应该是(注意_构造的变化):
您尚未将$color
对象传递给父类,并且宽度的拼写不正确
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;
}
您尚未将$color
对象传递给父类,并且宽度的拼写不正确
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;
}
哦,谢谢。这就是我想要的。我被理解了,但谢谢你)@Sorrybyenglish是的,它对变量名起作用,我的意思是在html输出中。检查你的回音内容。对不起,我弄错了。我忘了,对不起。我今年18岁,在6个月的时间里学习php。应该是家长::_construct($color);使用$this->color=$color;。这是工作。但只有父项::_构造($color);这不管用)哇!确切地但是组件如何与颜色类交互呢?哦,谢谢。这就是我想要的。我被理解了,但谢谢你)@Sorrybyenglish是的,它对变量名起作用,我的意思是在html输出中。检查你的回音内容。对不起,我弄错了。我忘了,对不起。我今年18岁,在6个月的时间里学习php。应该是家长::_construct($color);使用$this->color=$color;。这是工作。但只有父项::_构造($color);这不管用)哇!确切地但组件如何与颜色类交互?失败的是什么?是否打开了错误消息?页面是否无法完全加载?警告:缺少组件的参数1::uu construct(),在第26行的/public_html/index.php中调用,在第18行的/public_html/index.php中定义,您必须告诉我们(您自己也要理解!)什么不起作用,然后才能了解原因。通常第一个会直接引导你到第二个。它起作用了!!!感谢xdazz的函数和Starx的简单语法提示。失败的是什么?是否打开了错误消息?页面是否无法完全加载?警告:缺少组件的参数1::uu construct(),在第26行的/public_html/index.php中调用,在第18行的/public_html/index.php中定义,您必须告诉我们(您自己也要理解!)什么不起作用,然后才能了解原因。通常第一个会直接引导你到第二个。它起作用了!!!感谢xdazz函数和Starx提醒简单语法。@Sorrybyenglish,是的,检查整个函数中宽度的拼写。见update@Sorrybyenglish,是的,在整个函数中检查宽度的拼写。查看更新