doubengshao8872 2016-09-26 10:06
浏览 37
已采纳

什么是OOPS中的函数重载和覆盖,用外行术语解释[重复]

This question already has an answer here:

Actually i am new to OOPS concepts and it is hard to understand, and i also read it somewhere that "we do not have overloading in PHP".I am studying this example but it didnt get me somewhere.

<?php
class Toys{
    private $str;
    public function __set($name,$value){
        $this->str[$name] = $value;
    }

    public function __get($name){
        echo "Overloaded Property name = " . $this->str[$name] . "<br/>";
    }

    public function __isset($name){
        if(isset($this->str[$name])){
            echo "Property \$$name is set.<br/>";       
        } else {
            echo "Property \$$name is not set.<br/>";
        }
    }

    public function __unset($name){
        unset($this->str[$name]);
        echo "\$$name is unset <br/>";
    }
}

$objToys = new Toys;

/* setters and getters on dynamic properties */
$objToys->overloaded_property = "new";
echo $objToys->overloaded_property . "

";
/*Operations with dynamic properties values*/

isset($objToys->overloaded_property);
unset($objToys->overloaded_property);
isset($objToys->overloaded_property);
?>
</div>
  • 写回答

3条回答 默认 最新

  • dsp15140275697 2016-09-26 10:13
    关注

    Method Overriding

    Method Overloading

    Simple Explanation

    Method Overriding is when a method defined in a superclass or interface is re-defined by one of its subclasses, thus modifying/replacing the behavior the superclass provides. The decision to call an implementation or another is dynamically taken at runtime, depending on the object the operation is called from. Notice the signature of the method remains the same when overriding.

    Method Overloading is unrelated to polymorphism. It refers to defining different forms of a method (usually by receiving different parameter number or types). It can be seen as static polymorphism. The decision to call an implementation or another is taken at coding time. Notice in this case the signature of the method must change.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法