PHP面向对象编程之深入理解方法重载与方法覆盖(多态)_PHP
什么是多态?
多态(Polymorphism)按字面的意思就是“多种状态”。在面向对象语言中,接口的多种不同的实现方式即为多态。引用Charlie Calverts对多态的描述——多态性是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作(摘自“Delphi4编程技术内幕”)。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针(没错这段话来自百度百科)。那么多态的作用是什么,它有什么实际开发价值呢?在实际的应用开发中,采用面向对象中的多态主要在于可以将不同的子类对象都当作一个父类来处理,并且可以屏蔽不同子类对象之间所存在的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化。
下面就是PHP中多态的两个实现
方法重载(overload)
重载是类的多态的一种实现。函数重载指一个标识符被用作多个函数名,且能够通过函数的参数个数或参数类型将这些同名的函数区分开来,调用不发生混淆。即当调用的时候,虽然方法名字相同,但根据参数的不同可以自动调用相应的函数。
class A{ public function test(){ echo "test1"; } public function test($a){ echo "test2"; } } $a=new A(); $a->test(); $a->test($a);
假如php直接支持方法重载的话。那么上面的例子执行后传参和不传参就会返回不同的值。然而php并不直接支持重载,这就意味着你如果直接按上面这样定义的话,就会报错的。会报什么错呢?会报如下的错误。
这意思就是不能重复定义A函数,报错的行数也正是下面这行。
public function test($a){
所以说php是并不直接支持重载的。合着说了这么半天php并不支持。。别急,我说的是并不直接支持,所以说是我们可以让php间接支持。这时候就要用到一个函数来支持重载了。就是__call()。__call()方法必须带有两个参数。第一个包含了被调用的方法名称,而第二个参数包含了传递给该方法的参数数组。可以通过这个方法实现类似于函数重载的功能。看下面的代码。
public function __call($method,$p) { if($method=="display"){ if(is_object($p[0])){ $this->displayObject($p[0]); }else if(is_array($p[0])){ $this->displayArray($p[0]); }else{ $this->displayScalar($p[0]); } } } //下面是对上面定义的调用 $ov=new overload; $ov->display(array(1,2,3)); $ov->display('cat');
定义方法的时候,可以看到有三个分支,如果一个对象传递给display()方法,就调用的是displayObject()方法;如果传递的是一个数组,调用displayArray();传递的是其他的内容的话,则调用的是displayScalar()方法。。。可以看到下面调用时,第一个是传递了一个数组,则调用displayArray()。第二个传入的不是对象也不是数组,则属于其他内容,调用的是displayScalar()方法。所以这样子就用__call()方法实现了类似于其他语言的方法重载。
方法覆盖(override)
所谓覆盖,从本质上来说就是重写。就是当子类继承父类的一些方法后,子类又在其内部定义了相同的方法,则这个新定义的方法会覆盖继承而来的父类的方法,子类只能调用其内部定义的方法。
有以下几点要求:
1.当一个父类和子类有一个方法,参数和名字完全一致,那么子类方法会覆盖父类的方法。
2.在实行方法覆盖的时候,访问修饰符可以是不一样的,但是子类的访问范围必须大于等于父类的访问范围。
3.要求参数和名字一样。并不是要求子类,父类名称相同。
下面是对这几点的解释:
第一点,必须参数一致,才会实现方法覆盖。当参数个数不一致,则会报错(这就牵扯到上面说所得方法重载)。当方法名字不一致,就不会覆盖,只是子类新定义的方法。;
第二点,这是php这些语言设计时的规定吧。我是这么理解的是访问高一层的东西比较容易,如果再去访问底层的东西权限肯定要高一些。
看代码:
class people{ protected function sing(){ echo "人唱歌"; } } class woman extends people{ public function sing(){ echo "女人唱歌"; } } $woman1=new woman(); $woman1->sing();
这样很正常的可以输出“女人唱歌”。但当把woman里的sing()方法改为proctcted,父元素改成public()时,即将父类的访问权限设置的大于子类后,就会报下面的错误。
第三点,是要求参数和名字一样,具体就是要求参数的个数与父类相同,而并不是参数名称一致。即传递的参数名字可以为任意,只要保证传递的个数相同即可。
以上内容简单介绍了PHP语言中多态的两个实现。
PS:重写、覆盖、重载、多态几个概念的区别分析
override->重写(=覆盖)、overload->重载、polymorphism -> 多态
override是重写(覆盖)了一个方法,以实现不同的功能。一般是用于子类在继承父类时,重写(重新实现)父类中的方法。
重写(覆盖)的规则:
1、重写方法的参数列表必须完全与被重写的方法的相同,否则不能称其为重写而是重载.
2、重写方法的访问修饰符一定要大于被重写方法的访问修饰符(public>protected>default>private)。
3、重写的方法的返回值必须和被重写的方法的返回一致;
4、重写的方法所抛出的异常必须和被重写方法的所抛出的异常一致,或者是其子类;
5、被重写的方法不能为private,否则在其子类中只是新定义了一个方法,并没有对其进行重写。
6、静态方法不能被重写为非静态的方法(会编译出错)。
overload是重载,一般是用于在一个类内实现若干重载的方法,这些方法的名称相同而参数形式不同。
重载的规则:
1、在使用重载时只能通过相同的方法名、不同的参数形式实现。不同的参数类型可以是不同的参数类型,不同的参数个数,不同的参数顺序(参数类型必须不一样);
2、不能通过访问权限、返回类型、抛出的异常进行重载;
3、方法的异常类型和数目不会对重载造成影响;
多态的概念比较复杂,有多种意义的多态,一个有趣但不严谨的说法是:继承是子类使用父类的方法,而多态则是父类使用子类的方法。
一般,我们使用多态是为了避免在父类里大量重载引起代码臃肿且难于维护。
举个例子:
public class Shape { public static void main(String[] args){ Triangle tri = new Triangle(); System.out.println("Triangle is a type of shape? " + tri.isShape());// 继承 Shape shape = new Triangle(); System.out.println("My shape has " + shape.getSides() + " sides."); // 多态 Rectangle Rec = new Rectangle(); Shape shape2 = Rec; System.out.println("My shape has " + shape2.getSides(Rec) + " sides."); //重载 } public boolean isShape(){ return true; } public int getSides(){ return 0 ; } public int getSides(Triangle tri){ //重载 return 3 ; } public int getSides(Rectangle rec){ //重载 return 4 ; } } class Triangle extends Shape { public int getSides() { //重写,实现多态 return 3; } } class Rectangle extends Shape { public int getSides(int i) { //重载 return i; } }
注意Triangle类的方法是重写,而Rectangle类的方法是重载。对两者比较,可以发现多态对重载的优点:
如果用重载,则在父类里要对应每一个子类都重载一个取得边数的方法;
如果用多态,则父类只提供取得边数的接口,至于取得哪个形状的边数,怎样取得,在子类里各自实现(重写)。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In-depth interpretation of PHP object-oriented encapsulation Encapsulation is one of the three major characteristics of object-oriented programming. It refers to encapsulating data and operations on data in a class, hiding specific implementation details from external programs, and providing external interfaces. In PHP, the concept of encapsulation is implemented by using access modifiers (public, protected, private) to control the accessibility of properties and methods. First, let’s take a look at the role of access modifiers: public (public): Public properties and methods can

How to implement object version control and management through the PHP object-oriented simple factory model. When developing large and complex PHP projects, version control and management are very important. Through appropriate design patterns, we can better manage and control the creation and use of objects, thereby improving the maintainability and scalability of the code. This article will introduce how to use the PHP object-oriented simple factory pattern to implement object version control and management. The simple factory pattern is a design pattern for creating classes that instantiates specified objects through a factory class

How to create flexible object instances using the PHP object-oriented simple factory pattern. The simple factory pattern is a common design pattern that creates object instances without exposing object creation logic. This mode can improve the flexibility and maintainability of the code, and is especially suitable for scenarios where different objects need to be dynamically created based on input conditions. In PHP, we can use the characteristics of object-oriented programming to implement the simple factory pattern. Let's look at an example below. Suppose we need to create a graphing calculator that can

How to achieve seamless switching and replacement of objects through PHP's object-oriented simple factory pattern Introduction: In PHP development, object-oriented programming (Object-oriented Programming, referred to as OOP) is a very common programming paradigm. The object-oriented design pattern can further improve the maintainability and scalability of the code. This article will focus on the simple factory pattern in PHP to achieve seamless switching and replacement of objects. What is the simple factory pattern? Simple factory pattern (Simple

With the development of the Internet, PHP has gradually become one of the most popular programming languages in web development. However, with the rapid development of PHP, object-oriented programming has become one of the necessary skills in PHP development. In this article, we will discuss how to master object-oriented programming skills in PHP development. Understanding the Concepts of Object-Oriented Programming Object-oriented programming is a programming paradigm that organizes code and data through the use of objects (classes, properties, and methods). In object-oriented programming, code is organized into reusable modules, thereby improving the program's

Understanding the Object-Oriented Inheritance Mechanism of PHP Inheritance is an important concept in object-oriented programming, which allows the creation of new classes that include the features and functionality of the old classes. In PHP, inheritance can be achieved through the keyword extends. Through inheritance, subclasses can inherit the properties and methods of the parent class, and can add new properties and methods, or override inherited methods. Let us understand the object-oriented inheritance mechanism of PHP through an example. classAnimal{public$name

With the continuous development of Internet technology, PHP has become one of our common website development languages, and PHP object-oriented programming has also become a knowledge point that must be learned. Object-oriented programming (OOP) is a programming paradigm whose core concept is to combine data and behavior into an object to improve code reusability, readability, and maintainability. This article will explore how to use PHP to implement object-oriented programming and improve the readability and maintainability of your code. Basic Concepts of Object-Oriented Programming In object-oriented programming, each object has a set of properties.

PHP language has become a very popular web development language because it is easy to learn and use. Object-oriented programming is one of the most important programming paradigms in the PHP language. However, object-oriented programming is not something that is easy to master, so some common problems often arise. In this article, we will provide a detailed analysis of common problems with object-oriented programming in PHP. Question 1: How to create an object? In PHP, you can use the new keyword to create an object. For example: classMyClass{/
