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 關注
- 107 瀏覽
添加回答
舉報