이 코드에는 약 4 시간을 투자했지만 코드 조각이 정상적으로 실행되는 동안 필요한 결과를 얻지 못했습니다. 코드는 다음과 같습니다 :PHP 특성 :이 코드의 논리적 오류는 무엇입니까?
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() . "\n";
$cir = new Circle;
$cir->inputCircle(10);
Echo "Circle Area : " . $cir->area() . "\n";
이 코드의 논리 오류 란 무엇입니까? 내가 출력 다음 무엇입니까 이유 :
Rectangle Area : 0
Circle Area : 0
당신이 정의하고 GET을 사용하는 방법 ($의 prop) 기능이 내게 이상한 것처럼 보입니다. 너 왜 그거 해? 속성의 이름 충돌을 피하려면 적어도 해당 함수에서 이름 짓기를 수행해야합니다. 그렇지 않으면 get()을 사용하면 얻을 수있는 이점을 무시할 수 있습니다. – smarr