在代码片段正常运行期间,我投资了大约4个小时的代码,但没有获得要求的结果。
trait CircleShape{
public function input($radius){
$this->$radius = $radius;
}
}
trait AngleShape{
public function input($height, $width){
$this->$height = $height;
$this->$width = $height;
}
}
trait GeneralMethod{
public function get($property){
return $this->$property;
}
}
class Shape{
private $height, $width, $radius;
const PI = 3.1415;
use GeneralMethod, AngleShape, CircleShape{
AngleShape::input insteadof CircleShape;
CircleShape::input as inputCircle;
}
}
class Circle extends Shape{
public function area(){
return parent::PI * $this->get( radius ) * $this->get( radius );
}
}
class Rectangle extends Shape{
use GeneralMethod, AngleShape, CircleShape{
AngleShape::input insteadof CircleShape;
CircleShape::input as inputCircle;
}
public function area(){
return $this->get( height ) * $this->get( width );
}
}
$rect = new Rectangle;
$rect->input(12, 2);
Echo "Area: " . $rect->area() . "
";
$cir = new Circle;
$cir->inputCircle(10);
Echo "Circle Area : " . $cir->area() . "
";
What is the Logic Error in this Code? Why I am getting following Output:
Rectangle Area : 0
Circle Area : 0