PHP 中的抽象类是什么?
PHP 中的抽象类是什么?
如何使用?
What is an abstract class in PHP?
How can it be used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
PHP 中的抽象类是什么?
如何使用?
What is an abstract class in PHP?
How can it be used?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
抽象类是至少包含一个抽象方法的类,该方法中没有任何实际代码,只有名称和参数,并且已被标记为“抽象”。
这样做的目的是提供一种模板来继承并强制继承类实现抽象方法。
因此,抽象类介于常规类和纯接口之间。此外,接口是抽象类的一种特殊情况,其中所有方法都是抽象的。
请参阅 PHP 手册的本节以获取更多参考。
An abstract class is a class that contains at least one abstract method, which is a method without any actual code in it, just the name and the parameters, and that has been marked as "abstract".
The purpose of this is to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods.
An abstract class thus is something between a regular class and a pure interface. Also interfaces are a special case of abstract classes where ALL methods are abstract.
See this section of the PHP manual for further reference.
1.无法实例化抽象类:定义为抽象的类不能被实例化,并且任何至少包含一个抽象方法的类也必须是抽象的。
示例如下:
2.任何至少包含一个抽象方法的类也必须是抽象的:抽象类可以有抽象方法和非抽象方法,但它必须至少包含一个抽象方法。如果一个类至少有一个抽象方法,那么该类必须声明为抽象的。
示例如下:
3.抽象方法不能包含主体:定义为抽象的方法只是声明方法的签名 - 它们无法定义实现。但非抽象方法可以定义实现。
4.从抽象类继承时,父类声明中标记为抽象的所有方法都必须由子类定义:如果继承抽象类,则必须为其中的所有抽象方法提供实现。
5.相同(或较少限制)的可见性:从抽象类继承时,父类声明中标记为抽象的所有方法都必须由子类定义;此外,这些方法必须以相同(或较少限制)的可见性来定义。例如,如果抽象方法定义为 protected,则函数实现必须定义为 protected 或 public,但不能定义为 private。
6.抽象方法的签名必须匹配:从抽象类继承时,父类声明中标记为抽象的所有方法都必须由子类定义;方法的签名必须匹配,即类型提示和数量所需参数必须相同。例如,如果子类定义了可选参数,而抽象方法的签名没有定义可选参数,则签名中不存在冲突。
7.抽象类不支持多重继承:抽象类可以扩展另一个抽象类,抽象类可以提供接口的实现,但不支持多重继承。
下面的示例将导致致命错误:找不到“马”类
1. Can not instantiate abstract class: Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract.
Example below :
2. Any class that contains at least one abstract method must also be abstract: Abstract class can have abstract and non-abstract methods, but it must contain at least one abstract method. If a class has at least one abstract method, then the class must be declared abstract.
Example below :
3. An abstract method can not contain body: Methods defined as abstract simply declare the method's signature - they cannot define the implementation. But a non-abstract method can define the implementation.
4. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child :If you inherit an abstract class you have to provide implementations to all the abstract methods in it.
5. Same (or a less restricted) visibility:When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
6. Signatures of the abstract methods must match:When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child;the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature.
7. Abstract class doesn't support multiple inheritance:Abstract class can extends another abstract class,Abstract class can provide the implementation of interface.But it doesn't support multiple inheritance.
below example will cause Fatal error: Class 'horse' not found
对此有很好的解释此处。
There is good explanation of that here.
抽象类
1.包含抽象方法
2.无法直接初始化
3. 无法创建抽象类的对象
4. 仅用于继承目的
抽象方法
1. 不能包含正文
2. 不能定义为私有
3. 子类必须定义抽象类中声明的方法
示例代码:
输出:
Abstract Class
1. Contains an abstract method
2. Cannot be directly initialized
3. Cannot create an object of abstract class
4. Only used for inheritance purposes
Abstract Method
1. Cannot contain a body
2. Cannot be defined as private
3. Child classes must define the methods declared in abstract class
Example Code:
Output:
抽象类就像普通类一样,它包含变量,它包含受保护的变量,它包含构造函数,只有一件事不同,它包含抽象方法。
抽象方法是指没有定义的空方法,因此与抽象类的唯一区别是我们不能创建抽象类的对象
抽象必须包含抽象方法并且这些方法必须在其继承类中定义。
An abstract class is like the normal class it contains variables it contains protected variables functions it contains constructor only one thing is different it contains abstract method.
The abstract method means an empty method without definition so only one difference in abstract class we can not create an object of abstract class
Abstract must contains the abstract method and those methods must be defined in its inheriting class.
它涉及“合同协议”,但也涉及责任分配。如果您确实分配了任务,这一点就会变得很明显。
长期以来,我一直在努力理解抽象类,因为经常与解释一起发布的示例并没有充分说明抽象的必要性。但今天我有一个有用的用例,可能对阅读本文的人有所帮助:
It's about "contract agreements" – but also about a distribution of responsibilities. This becomes apparent if you actually do distribute the tasks.
I had long struggled to comprehend abstract classes because the examples often posted with the explanations didn't illustrate enough the necessity for abstraction. But today I've had a useful use case which might be helpful for whoever reads this: