如果可以使用接口和特征,为什么 PHP 有抽象类? [英] Why does PHP have abstract classes if you can use an interface and traits?

查看:29
本文介绍了如果可以使用接口和特征,为什么 PHP 有抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早些时候我正在研究 PHP 的抽象类接口traits.

Earlier today I was doing research on PHP's abstract classes, interfaces, and traits.

据我所知,抽象类说任何使用我的东西都将使用这些方法和属性",接口说任何使用我的东西都必须具有这些方法和属性",而特质说任何使用我的东西也将使用这些方法和属性"有这些方法和属性".

As far as I can tell, an abstract class says "anything using me will be using these methods and attributes", interfaces say "anything using me must have these methods and attributes", and traits say "anything using me will also have these methods and attributes".

现在,我的问题是,如果您在使用接口和特征时获得了抽象类的等价物,那为什么还有抽象类?

Now, my question is, if you get the equivalent of an abstract class when you use an interface and a trait, why are there abstract classes?

如果我错了,并且接口和特征不等同于抽象类,请您解释为什么不是这样?

If I'm wrong and an interface and a trait aren't the equivalent of an abstract class, can you please explain why that's not the case?

推荐答案

我认为关于如何以及何时使用它们存在一些哲学上的差异.

I think there is some philosophical difference on how and when to use them.

你说:

  1. 抽象类:任何使用我的都会使用这些方法和属性"
  2. 接口:任何使用我的必须这些方法和属性"
  3. 特征:任何使用我的也将具有这些方法和属性".
  1. abstract classes: "anything using me will be using these methods and attributes"
  2. interfaces:"anything using me must have these methods and attributes"
  3. traits: "anything using me will also have these methods and attributes".

如果您专注于自己的措辞,那是有道理的.

If you focus on your own wordings it makes sense.

抽象类实际上定义了抽象的东西,例如,Vehicle 是一个抽象的东西,直到或除非它具体化汽车或自行车.接口和特性都没有定义它.

Abstract Classes are in reality define things that are abstract e.g Vehicle is an abstract thing until or unless its materialized in the form of a car or a bike . Neither interface define it nor traits.

接口补充类继承功能,其中一个类从多个类继承(只有某些语言提供多重继承,例如 C/C++).接口,顾名思义,侧重于 INTERFACE ,而不是实现它的类中接口方法的实现.它使类 PLUG &PLAYABLE,所以每个人都应该遵循标准.如果您进一步阅读有关 OOP 的工厂和适配器模式,您就会理解它.

Interfaces compliment the class inheritance functionality where a class inherits from multiple classes(only certain languages provide multiple inheritance e.g C/C++). Interfaces , as names suggest focus on the INTERFACE , and not the implementation of the interface method in class which is implementing it. It makes classes PLUG & PLAYABLE so everyone should follow a standard. If you further read about factory and adapter pattern on OOP you will understand it.

特征 具有不绑定到特定类的实现/功能.相反,它可以在不同的类中找到.它就像遗传学中的一个基因,它在父母身上保持沉默,只出现在某些孩子身上.或者是简洁的选择性继承但不绑定到单个类.所以它提供了一种更好的代码重用

Traits have implementation/functionality that is not bound to specific classes.Instead it could be found across different classes. Its like a gene in genetics which stays mute in parents and appear only in certain children. Or to be concise selective inheritance but not bound to single class. So it provides a way much better code-reuse

编辑Interface + Trait != Abstract Class ,因为当您选择要使用的特定特征时,使用 Trait 继承是 selective 而使用 Abstract Class 继承是强制性的或由父类决定的没有自由!

Edit Interface + Trait != Abstract Class , because when using Trait inheritance is selective as you select specific trait to use and while using Abstract Class inheritance is mandatory or dictated by parent class you don't have freedom!

这篇关于如果可以使用接口和特征,为什么 PHP 有抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆