search
HomeBackend DevelopmentPHP TutorialPHP polymorphism, php polymorphism_PHP tutorial

PHP polymorphism, php polymorphism

1. What is polymorphism

Polymorphism is understood literally That’s “many shapes.” It can be understood as multiple forms of expression, that is, "one external interface and multiple internal implementation methods." In object-oriented theory, the general definition of polymorphism is: the same operation will produce different execution results when applied to instances of different classes. That is, when objects of different types receive the same message, they will get different results.

In actual application development, the main reason for using object-oriented polymorphism is that different subclass objects can be treated as one parent class, and the differences that exist between different subclass objects can be shielded. Differences, write common code, and make common programming to adapt to changing needs.

/**
* Shape Interface
*
* @version 1.0
* @copyright
* (1) Using interface, you can specify which methods a class must implement, but it is not required Define the specifics of these methods.
* (2) We can define an interface through interface, just like defining a standard class, but all the methods defined in it are empty.
* (3) All methods defined in the interface must be public, which is a characteristic of the interface
*/

interface Shape {
public function draw();
}

/**
* Triangle
*
* @uses Shape
* @version 1.0
* @copyright
* (1) To implement an interface, you can use the implements operator. The class must implement all methods defined in the interface, otherwise a fatal error will be reported.
* (2) If you want to implement multiple interfaces, you can use commas to separate the names of multiple interfaces.
*/
class Triangle implements Shape {
public function draw() {
print "Triangle::draw()n";
}
}

/ **
* Rectangle 

* @uses Shape
* @version 1.0
* @copyright
*/
class Rectangle implements Shape {
public function draw() {
print "Rectangle::draw()n";
}
}

/**
* Test Polymorphism

* @version 1.0
* @copyright
*/
class TestPoly {
public function drawNow($shape) {
$shape->draw();
}
}


$test = new TestPoly();
$test->drawNow(new Triangle());
$test->drawNow(new Rectangle());


?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1119060.htmlTechArticlePHP polymorphism, php polymorphism 1. What is polymorphism? Polymorphism (Polymorphism) literally means Various shapes. It can be understood as multiple forms of expression, that is, an external interface, multiple internal...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL中如何实现数据的多态存储和多维查询?MySQL中如何实现数据的多态存储和多维查询?Jul 31, 2023 pm 09:12 PM

MySQL中如何实现数据的多态存储和多维查询?在实际应用开发中,数据的多态存储和多维查询是一个非常常见的需求。MySQL作为常用的关系型数据库管理系统,提供了多种实现多态存储和多维查询的方式。本文将介绍使用MySQL实现数据的多态存储和多维查询的方法,并提供相应的代码示例,帮助读者快速了解和使用。一、多态存储多态存储是指将不同类型的数据存储在同一个字段中的技

如何在PHP中使用多态和继承来处理数据类型如何在PHP中使用多态和继承来处理数据类型Jul 15, 2023 pm 07:41 PM

如何在PHP中使用多态和继承来处理数据类型引言:在PHP中,多态和继承是两个重要的面向对象编程(OOP)概念。通过使用多态和继承,我们可以更加灵活地处理不同的数据类型。本文将介绍如何在PHP中使用多态和继承来处理数据类型,并通过代码示例展示它们的实际应用。一、继承的基本概念继承是面向对象编程中的一种重要概念,它允许我们创建一个类,该类可以继承父类的属性和方法

“PHP面向对象编程入门:从概念到实践”“PHP面向对象编程入门:从概念到实践”Feb 25, 2024 pm 09:04 PM

什么是面向对象编程?面向对象编程(OOP)是一种编程范式,它将现实世界中的实体抽象为类,并使用对象来表示这些实体。类定义了对象的属性和行为,而对象则实例化了类。OOP的主要优点在于它可以使代码更易于理解、维护和重用。OOP的基本概念OOP的主要概念包括类、对象、属性和方法。类是对象的蓝图,它定义了对象的属性和行为。对象是类的实例,它具有类的所有属性和行为。属性是对象的特征,它可以存储数据。方法是对象的函数,它可以对对象的数据进行操作。OOP的优点OOP的主要优点包括:可重用性:OOP可以使代码更

继承、多态与接口:PHP面向对象的三大特性继承、多态与接口:PHP面向对象的三大特性May 11, 2023 pm 03:45 PM

PHP是一种服务器端编程语言,自PHP5之后开始支持面向对象编程(OOP)。OOP的核心思想是将数据和行为封装在对象中,以提高程序的可维护性和可扩展性。在PHP中,面向对象编程具有三大特性:继承、多态与接口。一、继承继承是指一个类可以从另一个类中继承属性和方法。被继承的类称为父类或基类,继承的类称为子类或派生类。子类可以通过继承获得父类中的属性和方法,并且可

golang函数重载与多态的区别?golang函数重载与多态的区别?Apr 30, 2024 am 09:30 AM

Go语言中不支持函数重载,因为它采用鸭子类型,根据实际类型确定值类型。而多态则通过接口类型和方法调用实现,不同类别的对象可以以相同方式响应。具体来说,Go语言中通过定义接口并实现这些方法,可以使不同类型的对象拥有相似行为,从而支持多态。

PHP中的多态与派发机制的关系PHP中的多态与派发机制的关系Jul 07, 2023 pm 05:45 PM

PHP中的多态与派发机制的关系在面向对象编程中,多态是一种强大的概念,它允许不同的对象对同一消息做出不同的响应。PHP作为一门强大的开发语言,也支持多态性,并且与之紧密相关的是派发机制。本文将通过代码示例来探讨PHP中的多态与派发机制的关系。首先,我们来了解一下什么是多态。多态是指对象能够根据自己的实际类型来调用相应的方法。通过使用多态,程序可以根据具体对象

分析PHP面向对象编程中的多态与接口关系分析PHP面向对象编程中的多态与接口关系Aug 10, 2023 pm 06:09 PM

PHP面向对象编程中的多态与接口关系在PHP面向对象编程中,多态(Polymorphism)是一种重要的概念,它使得不同类的对象可以以一种统一的方式被使用。多态通过接口(Interface)的实现来实现,本文将通过代码示例来分析PHP面向对象编程中的多态与接口关系。在PHP中,接口是一种定义了一组方法的抽象结构,类通过实现接口来表达自己具有某些行为能力。接口

Go 语言中的多态和重载怎样实现?Go 语言中的多态和重载怎样实现?Jun 10, 2023 am 10:25 AM

Go语言作为一门静态类型语言,看似不能像动态语言那样实现多态和重载。但是,Go语言利用接口的特性实现了多态,而重载的实现则更加简单和精准。实现多态的方法Go语言中的接口可以在调用过程中实现多态,接口可以描述一个对象的行为,任何实现了接口所有方法的类型都可以称之为该接口类型的实例。通过这种方式,只需定义好接口类型,实现不同的具体类型,就可以实现多态。下面是一个

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.