简体   繁体   English

在这种情况下,我们需要在php中使用抽象类和接口

[英]In which case we need to use abstract class and interface in php

Abstract class abc{
---
}

I am new to OOP.I want to lean the object oriented programming concept. 我是OOP的新手,我想精简面向对象的编程概念。 There is a confusion between abstract classes and interfaces. 抽象类和接口之间存在混淆。 Where do we have to use this and which condition we have to you abstract and interface class. 我们必须在哪里使用此函数,以及我们必须为您提供哪种条件抽象和接口类。 Please suggest me tutorial links so that i can easily get the functionality. 请建议我教程链接,以便我可以轻松获得功能。 Suppose we are going to drink water because i need it. 假设我们要喝水,因为我需要水。 This is the situation when i need water so i take water. 这是我需要水时要喝水的情况。 In same case abstract and interface is needed. 在相同情况下,需要抽象和接口。

  • An abstract class is a special kind of class that cannot be instantiated. 抽象类是无法实例化的一种特殊类。 So the question is why we need a class that cannot be instantiated? 所以问题是为什么我们需要一个无法实例化的类? An abstract class is only to be sub-classed (inherited from). 抽象类仅是子类(从继承)。

    In other words, it only allows other classes to inherit from it but cannot be instantiated. 换句话说,它仅允许其他类从中继承而不能实例化。 The advantage is that it enforces certain hierarchies for all the subclasses. 优点是它为所有子类强制执行某些层次结构。 In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards. 简而言之,它是一种契约,它迫使所有子类进行相同的层次结构或标准。

  • An interface is not a class. 接口不是类。 It is an entity that is defined by the word Interface. 它是由接口一词定义的实体。 An interface has no implementation; 接口没有实现; it only has the signature or in other words, just the definition of the methods without the body. 它仅具有签名,换句话说,只有方法的定义而没有主体。

    As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. 作为与Abstract类的相似之处之一,它是用于定义所有子类的层次结构或定义特定的方法集及其参数的协定。 The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. 它们之间的主要区别是,一个类可以实现多个接口,但只能从一个抽象类继承。 Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance. 由于C#不支持多重继承,因此使用接口来实现多重继承。

Read more - http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface 阅读更多-http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM