Table of Contents
抽象类是一种特殊的类, 接口是一种特殊的抽象类, 而多态就要使用到抽象类或是接口
抽象类
什么是抽象方法?
什么是抽象类?
抽象类的作用:
接口
声明方式
接口和抽象类的对比
注意:
多态
Home Backend Development PHP Tutorial [PHP] 抽象类与接口

[PHP] 抽象类与接口

Jun 20, 2016 pm 12:29 PM

抽象类是一种特殊的类, 接口是一种特殊的抽象类, 而多态就要使用到抽象类或是接口

抽象类

什么是抽象方法?

定义:在一个类中,没有方法体的方法就是抽象方法(就是一个方法没有使用{}而直接使用分号结束)

abstract function test();  //抽象方法 function test(){  //有方法体,但方法体为空的 } 
Copy after login

如果一个方法是抽象方法,就必须使用abstract修饰为什么要使用抽象方法?

什么是抽象类?

  1. 如果一个类中,有一个方法是抽象的则这个类就是抽象类

  2. 如果一个类是抽象类,则这个类必须要使用abstract修饰

  3. 抽象类是一种特殊的类,就是因为一个类中有抽象方法,其他不变。也可以在抽象类中声明成员属性,常量,非抽象的方法。

  4. 抽象类不能实例化对象(不能通过抽象类去创建一个抽象类的对象)

//普通的类class 类名{   }// 抽象类abstract class 类名 {   } abstract class Demo{  var a;  abstract function fun1();   function fun2()  {   }} 
Copy after login

抽象类的作用:

  1. 要想使用抽象类,就必须使用一个类去继承抽象类,而且要想使用这个子类,也就是让子类可以创建对象,子类就必须不能再是抽象类,子类可以重写父类的方法(给抽象方法加上方法体)。抽象方法中的方法没有方法体, 子类必须实现这个方法 (父类中没写具体的实现, 但子类必须有这个方法名)

  2. 列用抽象类,可以定义一些规范,让子类按这些规范去实现自己的功能

接口

  • 接口是一种特殊的抽象类, 抽象类又是一种特殊的类

  • 接口和抽象类是一样的作用

  • 抽象类提供了具体实现的标准,而接口则是纯粹的模版。接口只定义功能,而不包含实现的内容。接口用关键字 interface 来声明。

  • 因为PHP是单继承的, 如果使用抽象类,子类实现完抽象类就不能再去继承其它的类了。如果既想实现一些规范, 又想继承一个其他类。就要使用接口

声明方式

interface 接口名{  const 常量名 = 常量值;  //接口中的属性必须为常量  function 函数名();}echo 接口名::常量名; interface Demo{  const HOST = "localhost";  function fun();}echoDemo::HOST; //输出为localhost 
Copy after login

接口和抽象类的对比

  1. 作用相同,都不能创建对象, 都需要子类去实现

  2. 接口的声明和抽象类不一样

  3. 接口被实现的方式不一样

  4. 接口中的所有方法必须是抽象方法,只能声明抽象方法(而且不需要使用abstract修饰)

  5. 接口中的成员属性,只能声明常量,不能声明变量

  6. 接口中的成员访问权限 都必须是public, 抽象类中最低的权限protected

  7. 使用一个类去实现接口, 不是使用extends关键字, 而是使用implements这个词

    • 如果子类是重写父类接口中抽象方法,则使用 implements : 类–接口, 抽象类—接口 implements 。接口—接口 extends

使用 implements 的两个目的

1. 可以实现多个接口 ,而 extends 词只能继承一个父类

2. extends 和 implements 两个可以同时使用

注意:

  • 可以使用抽象类去实现接口中的部分方法
  • 如果想让子类可以创建对象,则必须实现接口中的全部抽象方法

  • 可以定义一个接口去继承另一个接口

  • 一个类可以去实现多个接口(按多个规范去开发子类), 使用逗号分隔多个接口名称

  • 一个类可以在继承父类的同时,去实现一个或多个接口(先继承,再实现)

多态

多态是面向对象的三大特性之一,是面向对象设计的重要特性。它展现了动态绑定(dynamic binding)的功能,也称为同名异式”(Polymorphism)。多态的功能可让软件在开发和维护时,达到充分的延伸性(extension)。事实上,多态最直接的定义就是让具有继承关系的不同类对象,可以对相同名称的成员函数调用,产生不同的反应效果。

以下是关于使用多态的一个 Demo :

<html>  <head>  <metacharset="utf-8">  head>  </html><?php  interface USB  //声明一个USB接口  {    function mount();    function work();    function unMount();  }    class UPan implements USB  //UPan实现了USB的功能  {    function mount()    {      echo "U盘挂载<br>";    }    function work()    {      echo "U盘工作<br>";    }    function unMount()    {      echo "U盘卸载成功<br>";    }  }   class Wire implements USB  //数据线实现了USB的功能  {    function mount()    {        echo "数据线挂载<br>";    }    function work()    {        echo "开始传输数据<br>";    }    function unMount()    {        echo "数据线卸载成功<br>";    }  }  class PC  //声明一个PC类  {    function USB($usb)    {        $usb->mount();        $usb->work();        $usb->unMount();    }  }   class Person  //声明一个Person类  {    function usePC()    {      $pc = new PC;      $upan = new UPan;      $wire = new Wire;       $pc->USB($upan);      $pc->USB($wire);    }  }  $p1 = new Person;  //实例化一个Person类  $p1->usePC(); 
Copy after login

Statement of this Website
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles