在 php5 中重载父方法

发布于 2024-11-18 06:45:52 字数 2219 浏览 1 评论 0原文

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

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

我想重载 child2parent 的方法

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

提前致谢

I am struck at overloading the parent's class methods from an inherited child at level2.

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

I want to overload the methods of parent in 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();
?>

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

游魂 2024-11-25 06:45:52
$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 相同,但该方法无意打印参数。您使用接受的方法定义的数组没有任何作用。它只是一个数组。您必须向这些方法分配一些任务或像我一样返回一些错误。

$r->getSides(6,2);

Your abstract class demands three arguments! Plus the function is actually a setter method. You should name it setSides();. Same with getCoordinates().

Update: I think you are confusing inheritance with overloading. Here is an example for overloading with __call. I assume that's not what you are trying to do but what you have in your example. Maybe this will help.

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 />';
    }

}

Then if you do this:

$test = new overloadTestOne();

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

you will get this

// 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'

Although I have printOne and printTwo in the overload __call as accepted methods they are not used because these methods are already defined, they are handled by the existing methods as expected. On the other hand printThree gets overloaded because the method does not exist. Same with printFour but that method has no intend to print the argument. The array you have defined with the accepted methods doesn't do a thing. It is just an array. You have to assign some task to these methods or return some error like I did.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文