随笔 - 72,  文章 - 0,  评论 - 1,  阅读 - 22506

类的多态

多态性允许不同类的对象对相同的消息作出不同的响应。

多态性通过方法重写(覆盖)和方法重载来实现。

方法重写是指子类重写父类的方法,以改变方法的实现细节。

方法重载是指在同一个类中根据参数个数或类型不同来实现不同功能。

需要注意的是,多态性只适用于继承关系的类。子类必须重写父类的方法才能实现多态性。

 

 

在面向对象编程中,多态是指相同的操作或方法可以作用于不同的对象,从而产生不同的结果。

方法重写

方法重写 是子类提供对从父类继承的方法的特定实现。这是实现多态的一种方式。方法重写允许子类提供具体的实现,而不是使用父类中定义的实现。

示例:

 
<?php

class Animal {
    public function makeSound() {
        echo "Some generic animal sound";
    }
}

class Dog extends Animal {
    public function makeSound() {
        echo "Woof! Woof!";
    }
}

class Cat extends Animal {
    public function makeSound() {
        echo "Meow!";
    }
}

function printAnimalSound(Animal $animal) {
    $animal->makeSound();
}

// 创建不同类型的对象
$dog = new Dog();
$cat = new Cat();

// 调用函数,显示不同的声音
printAnimalSound($dog); // 输出: Woof! Woof!
printAnimalSound($cat); // 输出: Meow!

?>

在这个示例中,Animal 类定义了一个 makeSound 方法。DogCat 类重写了这个方法。printAnimalSound 函数接收一个 Animal 类型的参数,并调用 makeSound 方法。由于 makeSound 方法在 DogCat 类中有不同的实现,因此输出的声音也不同。

 

方法重载

复制代码
function test(){

    $args = func_get_args();  //打印参数具体内容,以数组形式展示

    $numArgs = func_num_args(); //打印参数的个数

    var_dump($args);
    var_dump($numArgs);

}

test(1,2,3,4);
复制代码

回显如下:

[Running] D:\ceshi.test\6.php"
array(4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
}
int(4)
也可以这样拿到具体的某一个参数
复制代码
function test(){

    $args = func_get_args();

    $numArgs = func_num_args();

    var_dump($args[0]);  //用index的方式拿到摸个具体的参数,这样会打印出1234中的1
    var_dump($numArgs);

}

test(1,2,3,4);
复制代码

 

我们将这2个函数应用到类中,如下:
 
复制代码
class Animal {
    public function makeSound() {
        echo "动物在吼叫";
    }
}

class Cat extends Animal {
    public function makeSound() {
        $numArgs = func_num_args(); // 获取参数数量
        
        switch ($numArgs) {
            case 2:
                echo '执行参数个数为2的事件';
                break;
            case 3:
                echo '执行参数个数为3的事件';
                break;
            default:
                echo '执行默认事件';
                break;
        }
    }
}

// 测试
$cat = new Cat();
$cat->makeSound(); // 执行默认事件
$cat->makeSound(1, 2); // 执行参数个数为2的事件
$cat->makeSound(1, 2, 3); // 执行参数个数为3的事件
复制代码

通过这样的方式来实现方法重载

附:

方法重写和方法重载是面向对象编程中的两个不同概念:

  1. 方法重写(Method Overriding):

    • 定义:子类提供对父类中已存在的方法的具体实现。
    • 特征:方法签名(名称、参数列表)必须与父类中的方法完全相同。
    • 目的:允许子类修改或扩展父类的行为。
    • 示例:
      phpCopy Code
      class Animal {
          public function makeSound() {
              echo "动物在吼叫";
          }
      }
      
      class Cat extends Animal {
          public function makeSound() {
              echo "猫在喵喵叫";
          }
      }
      
      $cat = new Cat();
      $cat->makeSound(); // 输出: 猫在喵喵叫
      
  2. 方法重载(Method Overloading):

    • 定义:在同一个类中定义多个同名方法,但参数列表不同。
    • 特征:允许方法有不同的参数列表(数量、类型)。
    • 目的:提供多种方法实现,以适应不同的参数输入。
    • 注意:PHP 不直接支持传统的重载方式,使用技巧如可变参数或 func_get_args() 来模拟。
    • 示例(模拟重载):
      class Example {
          public function display() {
              echo "没有参数";
          }
      
          public function displayWithParams($param1 = null, $param2 = null) {
              if ($param1 !== null && $param2 !== null) {
                  echo "两个参数: $param1, $param2";
              } elseif ($param1 !== null) {
                  echo "一个参数: $param1";
              } else {
                  echo "没有参数";
              }
          }
      }
      
      $ex = new Example();
      $ex->display(); // 输出: 没有参数
      $ex->displayWithParams("Hello"); // 输出: 一个参数: Hello
      $ex->displayWithParams("Hello", "World"); // 输出: 两个参数: Hello, World
      

在 PHP 中,由于语言本身的限制,方法重载通常通过参数处理和条件判断来模拟。

 
posted on   wilson'blog  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示