调用我的对象及其函数
Calling my objects and its functions
我是 php 的新手,正在尝试使用工厂方法。
我想使用 'build' 函数实例化我的 class 形状,这样当实例化 class 'Circle' 时计算其面积和周长,如果 class 'Rectangle' 被调用,它的面积和周长被计算并返回。
这就是我目前所拥有的和我想要的 achieve.This 不起作用,我相信有更好的方法来做到这一点!
$allmyshapes = Shapes::build($initData);
foreach($allmyshapes as $value) {
if ($value =='Circle'){
$x = new Circle();
echo $x::area();
echo $x::circumference();
}
if ($value =='Rectangle'){
$y = new Rectangle();
echo $y::area();
echo $y::circumference();
}
}
我的Class:
class Shapes {
public static function build($initData){
}
foreach($newlist as $value){
if($value[0]=='Circle'){
$shape1 =new Circle($value[1],$value[2]);
}
if($value[0]== 'Rectangle'){
$shape2 =new Rectangle($value[1],$value[2]);
}
}
return array($shape1,$shape2);
}
}
class Circle extends Shapes {
public $radius;
public $centre_point;
public function __construct($radius, $centre_point) {
$this->radius = $radius;
$this->centre_point = $centre_point;
}
public function area(){
return (pi() *$this->radius * $this->radius);
}
public function circumference(){
return 2 * pi() *$this->radius;
}
}
class Rectangle extends Shapes {
public $x;
public $y;
public function __construct($x, $y) {
$this->x= $x;
$this->y = $y;
}
public function area() {
return $this->x * $this->y;
}
public function circumference() {
return 2 * ($this->x+ $this->y);
}
}
你的类没问题,就是你用它们输出信息的方式:
class Shapes {
public static function build($initData){
//$initdata is a heredoc
$newlist = explode("\n", $initData);
foreach($newlist as $key => $initData){
$newlist[$key] = explode("\t", $initData);
}
foreach($newlist as $value){
if($value[0] == 'Circle'){
$shape1 = new Circle($value[1], $value[2]);
}
if($value[0] == 'Rectangle'){
$shape2 = new Rectangle($value[1], $value[2]);
}
}
return array($shape1, $shape2);
}
}
class Circle extends Shapes {
public $radius;
public $centre_point;
public function __construct($radius, $centre_point){
$this->radius = $radius;
$this->centre_point = $centre_point;
}
public function area(){
return (pi() * $this->radius * $this->radius);
}
public function circumference(){
return 2 * pi() * $this->radius;
}
public function draw(){
return ( $this->radius.":".$this->centre_point);
}
}
class Rectangle extends Shapes {
public $x;
public $y;
public function __construct($x, $y){
$this->x = $x;
$this->y = $y;
}
public function area(){
return $this->x * $this->y;
}
public function circumference(){
return 2 * ($this->x + $this->y);
}
}
$initData = <<<ENDINIT
Circle 5 2
Rectangle 5 10
Ellipse 10 10 4 5
ENDINIT;
$allmyshapes = Shapes::build($initData);
foreach($allmyshapes as $value){
if(get_class($value) === 'Circle'){
echo nl2br("**Circle**\nArea: ".$value->area()."\nCircumference: ".$value->circumference()."\n");
}
if(get_class($value) === 'Rectangle'){
echo nl2br("**Rectangle**\nArea: ".$value->area()."\nCircumference: ".$value->circumference()."\n");
}
}
我在您提供的代码中添加了 2 个函数,get_class()
& nl2br()
。如果您以 CLI 格式使用它,则可以删除 nl2br
,但它的主要功能是在使用 Web 浏览器查看输出时提高输出可读性。
get_class()
将解决您试图弄清楚在创建对象时使用了哪个构造函数的问题。
我是 php 的新手,正在尝试使用工厂方法。 我想使用 'build' 函数实例化我的 class 形状,这样当实例化 class 'Circle' 时计算其面积和周长,如果 class 'Rectangle' 被调用,它的面积和周长被计算并返回。 这就是我目前所拥有的和我想要的 achieve.This 不起作用,我相信有更好的方法来做到这一点!
$allmyshapes = Shapes::build($initData);
foreach($allmyshapes as $value) {
if ($value =='Circle'){
$x = new Circle();
echo $x::area();
echo $x::circumference();
}
if ($value =='Rectangle'){
$y = new Rectangle();
echo $y::area();
echo $y::circumference();
}
}
我的Class:
class Shapes {
public static function build($initData){
}
foreach($newlist as $value){
if($value[0]=='Circle'){
$shape1 =new Circle($value[1],$value[2]);
}
if($value[0]== 'Rectangle'){
$shape2 =new Rectangle($value[1],$value[2]);
}
}
return array($shape1,$shape2);
}
}
class Circle extends Shapes {
public $radius;
public $centre_point;
public function __construct($radius, $centre_point) {
$this->radius = $radius;
$this->centre_point = $centre_point;
}
public function area(){
return (pi() *$this->radius * $this->radius);
}
public function circumference(){
return 2 * pi() *$this->radius;
}
}
class Rectangle extends Shapes {
public $x;
public $y;
public function __construct($x, $y) {
$this->x= $x;
$this->y = $y;
}
public function area() {
return $this->x * $this->y;
}
public function circumference() {
return 2 * ($this->x+ $this->y);
}
}
你的类没问题,就是你用它们输出信息的方式:
class Shapes {
public static function build($initData){
//$initdata is a heredoc
$newlist = explode("\n", $initData);
foreach($newlist as $key => $initData){
$newlist[$key] = explode("\t", $initData);
}
foreach($newlist as $value){
if($value[0] == 'Circle'){
$shape1 = new Circle($value[1], $value[2]);
}
if($value[0] == 'Rectangle'){
$shape2 = new Rectangle($value[1], $value[2]);
}
}
return array($shape1, $shape2);
}
}
class Circle extends Shapes {
public $radius;
public $centre_point;
public function __construct($radius, $centre_point){
$this->radius = $radius;
$this->centre_point = $centre_point;
}
public function area(){
return (pi() * $this->radius * $this->radius);
}
public function circumference(){
return 2 * pi() * $this->radius;
}
public function draw(){
return ( $this->radius.":".$this->centre_point);
}
}
class Rectangle extends Shapes {
public $x;
public $y;
public function __construct($x, $y){
$this->x = $x;
$this->y = $y;
}
public function area(){
return $this->x * $this->y;
}
public function circumference(){
return 2 * ($this->x + $this->y);
}
}
$initData = <<<ENDINIT
Circle 5 2
Rectangle 5 10
Ellipse 10 10 4 5
ENDINIT;
$allmyshapes = Shapes::build($initData);
foreach($allmyshapes as $value){
if(get_class($value) === 'Circle'){
echo nl2br("**Circle**\nArea: ".$value->area()."\nCircumference: ".$value->circumference()."\n");
}
if(get_class($value) === 'Rectangle'){
echo nl2br("**Rectangle**\nArea: ".$value->area()."\nCircumference: ".$value->circumference()."\n");
}
}
我在您提供的代码中添加了 2 个函数,get_class()
& nl2br()
。如果您以 CLI 格式使用它,则可以删除 nl2br
,但它的主要功能是在使用 Web 浏览器查看输出时提高输出可读性。
get_class()
将解决您试图弄清楚在创建对象时使用了哪个构造函数的问题。