如果我在php中使用抽象类,有什么优势? [重复]

如何解决如果我在php中使用抽象类,有什么优势? [重复]

|                                                                                                                   这个问题已经在这里有了答案:                                                      

解决方法

           如果我在php中使用抽象类,有什么优势?我找不到任何好的东西。我想我可以轻松地使用抽象类来完成所有工作? 您自然可以。但是,如果有许多对象几乎具有相同的类型,则可能有助于将常用功能提取到\“ base \”类中,这意味着您不必重复该逻辑。 但是实际上有两个原因。对我而言,第一个原因是您的抽象类的所有后代都具有相同的类型,并且都遵循完全相同的接口。例如,这意味着PDF文档将具有与docx文档相同的界面,并且客户端代码不必关心它处理的是哪个对象。简短示例(在PHP中)。
<?php
abstract class Document {
    protected $author;

    public function __construct( $author ) {
        $this->author = $author;
    }

    abstract public function render( );

    public function author( ) {
        return $this->author;
    }
}

class PdfDocument extends Document {
    public function render( ) {
        // do something PDF specific here.
    }
}

class DocxDocument extends Document {
    public function render( ) {
        // do something DOCX specific here.
    }
}


class DocumentHandler {
    public function handle( Document $document ) {
        $this->log( \'Author has been read \' . $document->author( ) );
        return $document->render( );
    }
}
首先;请注意,DocumentHandler类不知道它实际处理的是哪种类型的文档。甚至都不在乎。很无知。但是,它确实知道可以调用哪些方法,因为两种类型的文档之间的接口是相同的。这称为多态,可以通过实现Document接口轻松实现。 第二部分是:如果每个文档都有一个作者,并且总是要求该作者,则可以将该方法复制到PdfDocument和DocxDocument中,但是您需要复制自己。例如,如果您决定要作者用大写字母书写,并且将$ this-> author返回给ucwords($ this-> author),则必须执行多次。您已经复制了该方法。使用抽象类,您可以定义行为,同时将类本身标记为不完整。这非常方便。 希望能有所帮助。     ,        当您有许多具有相同方法的类时,抽象类将为您提供帮助。 例:
abstract class Foo {
  public function foo1() {
    //Do something
  }

  public abstract class foo2();
}

class Bar extends Foo {
  public class foo2() {
    //Do something
  }
}

class Baz extends Foo {
}
会发生什么: 您不能使用
new Foo();
Foo
是抽象的。 您将可以使用
Bar.foo1()
Baz.foo1()
,它们将执行相同的操作。 您会遇到错误,因为
Baz
未实现表象思维方法
foo2
。 有用的示例:
abstract class Shape {
  public function display() { /* ... */ }
  //...
}

class Circle extends Shape {
  //...
}

class Rectangle extends Shape {
  //...
}

//...
您希望每个班级都能达到9分,但是本身就没有“形状”这样的东西。     ,        并非所有抽象类的方法都必须为空,可能有一些基本方法(和属性)可以使用。例如,您拥有一个和网上商店,您开发了一个抽象类来导入产品。该类具有将产品保存到db,生成产品的url的方法以及从某个地方检索产品的抽象方法(因此必须在扩展类中实现)。 接口只有空白的方法,没有属性(尽管可以有常量),所以接口可能没有实际的逻辑,只是方法常量,方法名称及其访问修饰符。     ,        顾名思义,接口的目的是显式声明由类和实例提供的数据和方法的接口,而无需立即对这些方法进行编码。经典的例子是几何形状的例子。这样的接口可以定义产生这种形状的区域的方法:
interface Shape {
    public function getArea();
}
可能有许多不同的类实现此接口,如
Circle
Square
,它们将为方法
getArea()
提供不同的实现。然后,您可以实现一个功能,以任何几何形状显示信息:
function displayInformation(Shape $shape) {
    echo \"The area of this shape is: \" . $shape->getArea();
}
您可以将实现
Shape
接口的任何对象传递给此函数,并且该接口保证存在
getArea()
方法。 当然,这些概念在比PHP更强类型化的编程语言中可能更有用。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-