2011-07-01 85 views
-1

我很驚訝從級別2的繼承子級重載父級的方法。在php5中重載父級的方法

abstract class parent 
    -> child1 extends parent 
    -> final class child2 extends child1 

我想重載的parent的方法child2

abstract class Shape 
{ 
    protected $length; 
    protected $height; 
    protected $a; 
    protected $b; 
    protected $c; 
    public function getCoordinates($length,$height) 
    { 
     $this->length=$length; 
     $this->height=$height; 
    } 
    public function getSides($a,$b,$c) 
    { 
     $this->a=$a; 
     $this->b=$b; 
     $this->c=$c; 
    } 

    abstract public function area(); 
    abstract public function perimeter(); 
    abstract public function display(); 

} 

class rectangle extends Shape 
{ 
    public function area() 
    { 
     return round(($this->length)*($this->height),2); 
    } 
    public function perimeter() 
    { 
     return round(2*(($this->a)+($this->b)),2); 
    } 
    public function display() 
    { 
     echo "area is :". rectangle::area() . "<br>"; 
     echo "perimeter is : ". rectangle::perimeter() ."<br>"; 
    } 
} 



final class triangle extends rectangle 
{ 
    function __call($method_name, $arguments) // this is wrong ........please modify here to call area(),which is in shape class(); 
     { 
      $accepted_methods = array("getCoordinates","area","perimeter"); 
     } 

     public function area() 
    { 
     return round((($this->length)*($this->height)*($this->width)/2),2); 
    } 
    public function perimeter() 
    { 
     return round((($this->a)+($this->b)+($this->c)),2); 
    } 
    public function getCoordinates($length,$height,$width) 
    { 
     $this->length=$length; 
     $this->height=$height; 
     $this->width=$width; 
     } 
    public function display() 
    { 
     echo "area is :". triangle::area() . "<br>"; 
     echo "perimeter is : ". triangle::perimeter() ."<br>"; 
    } 
} 


$r=new rectangle(); 
$r->getCoordinates(1,2,4); 
$r->getSides(6,2); 
$r->display(); 


$ot = new triangle(); 
$ot->getCoordinates(1,2,4); 
$ot->getSides(6,2,3); 
$ot->display(); 
?> 

在此先感謝

+3

最新情況? – KingCrunch

+0

那麼問題是什麼?你能提供一個簡化的代碼示例嗎? – lonesomeday

+0

難道你不知道如何從父類中調用方法嗎? –

回答

0
$r->getSides(6,2); 

你的抽象類需要三個參數!再加上這個函數實際上是一個setter方法。你應該把它命名爲setSides();.與getCoordinates()一樣。

更新:我認爲你是混淆繼承與重載。這是一個用__call重載的例子。我認爲這不是你想要做的,而是你的例子。也許這會有所幫助。

abstract class overloadTestAbstract { 

    public function printOne($show) { 
     echo __METHOD__ . ': ' . $show . '<br />'; 
    } 

} 

class overloadTestOne extends overloadTestAbstract { 

    public function __call($method,$arguments) { 
     $methods = array('printOne','printTwo','printThree'); 
     if (in_array($method,$methods)) { 
      echo __METHOD__ . ' :OVERLOAD METHOD: ' . $arguments[0] . '<br />'; 
     } else { 
      echo 'We are so sorry, but this method is available'; 
     } 
    } 

    public function printTwo($show) { 
     echo __METHOD__ . ': ' . $show . '<br />'; 
    } 

} 

然後,如果你這樣做:

$test = new overloadTestOne(); 

$test->printOne('Hello World'); 
$test->printTwo('Goodbye World'); 
$test->printThree('Hello World, again'); 
$test->printFour('Goodbye World, again'); 

你會得到這個

// print results 
'overloadTestAbstract::printOne: Hello World' 
'overloadTestOne::printTwo: Goodbye World' 
'overloadTestOne::__call :OVERLOAD METHOD: Hello World, again' 
'We are so sorry, but this method is available' 

雖然我有printOne和printTwo在過載__call爲接受的方法,他們不使用,因爲這些方法已經定義,它們按照預期由現有方法處理。另一方面,printThree由於方法不存在而被重載。與printFour一樣,但該方法無意打印參數。您使用接受的方法定義的數組不會執行任何操作。它只是一個數組。你必須爲這些方法分配一些任務,或像我一樣返回一些錯誤。

+0

問題很簡單::如何從子類中OVERLOAD父方法...?它不呼叫........我必須超載.....先生felix國王 – leela

+0

你正在調用方法getCoordinates()和getSides()兩次!這些方法已經存在,在您的抽象類中定義。因此__call重載不起作用。在調用對象上下文中的**不可訪問的**方法時觸發__call()。盡我所知,你的方法是可以訪問的! –