即时通讯尝试从称为方形功能区的另一个类调用一个函数到长方体类。来自另一个类别的呼叫功能
abstract class Shapes
{
protected $name;
protected $colour;
function __construct($n, $c)
{
$this->name = $n;
$this->colour = $c;
}
谢谢!
即时通讯尝试从称为方形功能区的另一个类调用一个函数到长方体类。来自另一个类别的呼叫功能
abstract class Shapes
{
protected $name;
protected $colour;
function __construct($n, $c)
{
$this->name = $n;
$this->colour = $c;
}
谢谢!
首先:至少你应该解决这个问题的方法:
function callclassA() {
$area1=0;
$classA = new Square();
$area1 = $area1 + $classA->area();
}
这并不意味着整个工作,但至少你会不会尝试调用非对象的方法。
第二个,callclassA()方法创建并填充变量,但它没有返回任何内容,也不会将结果保存在类变量中。这将是更好的尝试喜欢的东西
class Cuboid extends Shapes
{
private $square=null;
private $area=null;
function __construct($n, $c, $s, $ns)
{
parent::__construct($n, $c);
$this->square=new Square("Square", $c, $s, $ns);
$this->area = $this->square->area();
}
public function area()
{
return (6* $this->area);
}
public function perimeter()
{
return (9* $this->area);
}
}
三:你确定长方体的周长的正方形面积的9倍?不应该是平方英尺的东西?
感谢您的回复。我似乎得到这个错误解析错误:语法错误,意外的'$平方'(T_VARIABLE),期待函数(T_FUNCTION)。所以这是类Cuboid extends Shapes下面的行。谢谢 编辑:你是对的周边...我现在只是增加了一个随机值,因为我无法得到该地区的工作。所以虽然我的目标应该是先让区域工作 – user3320800
我的不好。缺乏能见度的班级变量。现在试试,我已经预先“私人”给他们。 – amenadiel
到达那里..现在我得到一个错误Square :: __构造(),缺少参数1 - 在方型函数构造和长方体类中的父构造..感谢 – user3320800
独立班不应该共享数据,这是不可能的任何理智的方式。相反,提供适配器的方法来从一个正方形转换成长方体:
class Square {
protected $s;
...
public function getSideLength() {
return $this->s;
}
}
class Cuboid {
...
public static function fromSquare(Square $square) {
return new static($square->getSideLength());
}
}
$square = new Square(...);
$cube = Cuboid::fromSquare($square);
你的代码太令人费解到它的细节调整,但这种跨越得到希望的想法。
嗨,ive更新了我的主要帖子与编辑3.是,它应该是什么样子。我做了正确的。谢谢 – user3320800
其中是您的printShape方法? –
将“Square”传递给Square类和“Cuboid”传递给Cuboid类的目的是什么? –
感谢您的回复.. ive更新了我的主帖。 ive在顶部添加了代码。打印功能在抽象类 – user3320800