1 回答
TA贡献1843条经验 获得超7个赞
您永远不会为类或接口分配访问修饰符。它们仅用于指定方法和属性。你在课堂上还有两个错误,Rectangle你应该提到高度和宽度
$this->height=$h;
$this->width=$w;
将您的整体代码更改为
<?php
interface Shape{
public function calculateArea();
}
class Circle implements Shape{
private $radius;
public function __construct($r){
$this->radius=$r;
}
public function calculateArea(){
echo 'Area of circle = '.pi()* $this->radius*$this->radius.'<br>';
}
}
class Rectangle implements Shape{
private $height;
private $width;
public function __construct($h,$w){
$this->height=$h;
$this->width=$w;
}
public function calculateArea(){
echo 'Area of a Rectangle=' .$this->height.$this->width.'<br>';
}
}
$circle= new Circle(5);
$rect= new Rectangle(10,20);
$circle->calculateArea();
$rect->calculateArea();
?>
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报