【问题标题】:overloading a parent's method in php5在php5中重载父母的方法
【发布时间】:2011-07-01 22:04:33
【问题描述】:

我对从 level2 继承的子级重载父级的类方法感到震惊。

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

我想在child2中重载parent的方法

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();
?>

提前致谢

【问题讨论】:

  • 那么问题是什么?能否提供一个简化的代码示例?
  • 你不知道怎么调用父类的方法吗?
  • 另外,“三角形延伸矩形”。现在,我已经有一段时间没有研究几何了,但这似乎不太可能......
  • 这是一个例子,不是纯几何先生。

标签: php methods overloading


【解决方案1】:
$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'

虽然我在重载 __call 中将 printOne 和 printTwo 作为接受的方法,但它们没有被使用,因为这些方法已经定义,它们按预期由现有方法处理。另一方面 printThree 被重载,因为该方法不存在。与 printFour 相同,但该方法无意打印参数。您使用接受的方法定义的数组没有任何作用。它只是一个数组。您必须像我一样为这些方法分配一些任务或返回一些错误。

【讨论】:

  • 问题很简单:: 如何从子类中重载父类的方法...??它没有打电话............我必须超载......菲利克斯国王先生
  • 您正在调用方法 getCoordinates() 和 getSides() 两次!这些方法已经存在,在您的抽象类中定义。因此,使用 __call 重载无效。 __call() 在对象上下文中调用 inaccessible 方法时触发。据我了解,您的方法可以访问!
猜你喜欢
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 2023-04-08
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
相关资源
最近更新 更多