即時通訊嘗試從稱爲方形功能區的另一個類調用一個函數到長方體類。來自另一個類別的呼叫功能
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