【问题标题】:Why Objective-C,PHP do not support method overloading? [duplicate]为什么 Objective-C、PHP 不支持方法重载? [复制]
【发布时间】:2013-08-21 09:42:25
【问题描述】:

显然objective-C不支持函数/方法重载,和php一样。但是任何人都知道为什么这些语言不支持此功能。

【问题讨论】:

  • PHP 支持使用魔术方法进行方法重载。
  • PHP 支持它,但与众所周知的标准不同
  • 在像 PHP 这样的松散类型语言的上下文中,函数重载基本上是无关紧要的,因为参数可以具有任何数据类型:即使现在,尽管 PHP 具有对象和数组的类型提示,但标量可以'没有类型提示(目前正在考虑),所以函数重载根本不切实际

标签: php objective-c


【解决方案1】:

Objective-C 不支持重载为explained in that post

PHP5支持重载

您需要 PHP 版本 > 5.1.0
请参阅 PHP 文档:http://php.net/manual/en/language.oop5.overloading.php

【讨论】:

    【解决方案2】:

    实际上 PHP 确实支持函数重载,但方式不同。 PHP 的 overloading features 与 Java 不同:

    PHP 对“重载”的解释与大多数面向对象的语言不同。传统上,重载提供了拥有多个具有相同名称但参数数量和类型不同的方法的能力。

    检查以下代码块。

    求n个数之和的函数:

    function findSum() {
        $sum = 0;
        foreach (func_get_args() as $arg) {
            $sum += $arg;
        }
        return $sum;
    }
    
    echo findSum(1, 2), '<br />'; //outputs 3
    echo findSum(10, 2, 100), '<br />'; //outputs 112
    echo findSum(10, 22, 0.5, 0.75, 12.50), '<br />'; //outputs 45.75
    Function to add two numbers or to concatenate two strings:
    
    function add() {
        //cross check for exactly two parameters passed
        //while calling this function
        if (func_num_args() != 2) {
            trigger_error('Expecting two arguments', E_USER_ERROR);
        }
    
        //getting two arguments
        $args = func_get_args();
        $arg1 = $args[0];
        $arg2 = $args[1];
    
        //check whether they are integers
        if (is_int($arg1) && is_int($arg2)) {
            //return sum of two numbers
            return $arg1 + $arg2;
        }
    
        //check whether they are strings
        if (is_string($arg1) && is_string($arg2)) {
            //return concatenated string
            return $arg1 . ' ' . $arg2;
        }
    
        trigger_error('Incorrect parameters passed', E_USER_ERROR);
    }
    
    echo add(10, 15), '<br />'; //outputs 25
    echo add("Hello", "World"), '<br />'; //outputs Hello World
    

    面向对象的方法,包括方法重载:

    PHP 中的重载提供了动态“创建”属性和方法的方法。这些动态实体是通过可以在一个类中为各种动作类型建立的魔术方法进行处理的。

    参考:http://php.net/manual/en/language.oop5.overloading.php

    在 PHP 中,重载意味着您​​可以在运行时添加对象成员,方法是实现一些魔术方法,例如 __set__get__call 等。

    类 Foo {

    public function __call($method, $args) {
    
        if ($method === 'findSum') {
            echo 'Sum is calculated to ' . $this->_getSum($args);
        } else {
            echo "Called method $method";
        }
    }
    
    private function _getSum($args) {
        $sum = 0;
        foreach ($args as $arg) {
            $sum += $arg;
        }
        return $sum;
    }
    
    }
    
    $foo = new Foo;
    $foo->bar1(); // Called method bar1
    $foo->bar2(); // Called method bar2
    $foo->findSum(10, 50, 30); //Sum is calculated to 90
    $foo->findSum(10.75, 101); //Sum is calculated to 111.75
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2011-03-17
      • 1970-01-01
      • 2011-03-24
      • 2011-04-03
      • 2017-01-11
      • 2011-02-21
      相关资源
      最近更新 更多