search
Homephp教程php手册举例讲解PHP面对对象编程的多态

这篇文章主要介绍了举例讲解PHP面对对象编程的多态,是PHP入门学习中的基础知识,需要的朋友可以参考下

什么是多态?
多态性,其来自于dictionary.com的定义是"以不同形式,阶段或者类型出现在独立的组织中或者同种组织中,而不存在根本区别。"由该定义,我们可以认为,多态性是一种通过多种状态或阶段来描述相同对象的编程方式。其实,它的真正意义在于:实际开发中,我们只需要关注一个接口或基类的编程,而不必担心一个对象所属于的具体类(class)。
如果你熟悉设计模式,即使只是有个初步了解,那么你也会了解这个概念。事实上,PHP5多态性可能是基于模式设计编程中的最伟大的工具。它允许我们以一种逻辑的方式来组织相类似的对象从而实现在具体编码时不必担心对象的具体类型;而且,,我们只需要对一个所期望的接口或基类编程即可。一个应用程序越抽象,则它就显得越灵活--而多态性是对行为加以抽象的最好的方式之一。
例如,让我们考虑一个叫Person的类。我们可以用称为David,Charles和Alejandro的类来子类化Person。Person有一个抽象方法AcceptFeedback(),所有的子类都要实现这个方法。这意味着,任何使用基类Person的子类的代码都能调用方法AcceptFeedback()。你不必检查该对象是一个David还是一个Alejandro,仅知道它是一个Person就够了。结果是,你的代码只需关注"最小公分母"-Person类即可。
在这个示例中的Person类也可以被创建为一个接口。当然,与上面相比存在一些区别,主要在于:一个接口并没有给出任何行为,而仅确定了一组规则。一个Person接口要求的是"你必须支持AddFeedback()方法",而一个Person类可以提供一些AddFeedback()方法的缺省代码-你对之的理解可以是"如果你不选择支持AddFeedback(),那么你应该提供一种缺省实现。"至于如何选择接口或基类则并非本文的主题;但是,一般说来,你需要通过基类来实现一个缺省的方法。如果你能够简单地勾勒出你的类所要实现的一组期望的功能,那么你也可以使用一个接口。

通俗理解
多态最直接的定义就是让具有继承关系的不同类对象,可以对相同名称的成员函数调用,产生不同的反应结果

多态的代码

   

fun1(); // wangzhengyi就读于中国传媒大学 $one->fun2(); // wangzhengyi的年龄是25 echo "
"; $two = new Two(); $two->fun1(); // wangzhengyi在北京灵创众和科技有限公司实习中 $two->fun2(); // wangzhengyi去年的年龄是24

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

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

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software