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

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

问题描述

今天早些时候我正在研究PHP的抽象类界面,以及特征



尽我所能告诉我们,一个抽象类说任何使用我的东西都会使用这些方法和属性,接口说任何使用我的东西都必须有这些方法和属性,而且特征说任何使用我的东西都会有这些方法和属性。 / p>

现在,我的问题是,如果在使用接口和特征时得到等效的抽象类,为什么会有抽象类?



如果我错了,接口和特性不等同于抽象类,你能解释为什么不是这样吗?

解决方案

我认为在如何以及何时使用它们方面存在一些哲学上的差异。



你说:


  1. 抽象类:使用我的任何东西使用这些方法
    和属性

  2. 接口:任何使用我的东西必须这些方法和属性

  3. 特征:使用我的任何内容具有这些方法和属性。

如果你专注于你自己的措辞,这是有道理的。



抽象类实际上是定义抽象的东西,例如,汽车是一种抽象的东西,直到或除非它<强烈的>物化的以汽车或自行车的形式。两个界面都没有定义它或特征。



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



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



编辑
接口+特性!=摘要类,因为当您选择要使用的特定特征时使用特征继承是 selective ,而使用抽象类继承是强制性的或者由父母阶级决定你没有自由!


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.

You said :

  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.

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.

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

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天全站免登陆