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条)

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?