如果我在php中使用抽象类有什么好处?
Possible Duplicate:
interface vs abstract class
What are the advantage if i use
abstract class in php?
What is the aim if i use abstract
class or interfaces?
Both are simply creating defenition
names with out body
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你当然可以。但是,如果有许多类型几乎相同的对象,则可能有助于将通用功能提取到“基”类中,这意味着您不必重复该逻辑。
但实际上有两个原因。对我来说,第一个原因是抽象类的所有后代都具有相同类型,并且都遵循完全相同的接口。这意味着 PDF 文档将具有与 docx 文档相同的接口,并且客户端代码不必关心它正在处理哪个对象。简短示例(PHP 语言)。
首先;请注意 DocumentHandler 类不知道它实际处理的文档类型。它甚至不在乎。这是无知的。但是,它确实知道可以调用哪些方法,因为两种类型的文档之间的接口是相同的。这称为多态性,并且可以通过实现 Document 接口轻松实现。
第二部分是;如果每个文档都有一个作者,并且总是要求该作者,您可以将该方法复制到 PdfDocument 和 DocxDocument,但您会重复自己。例如,如果您决定希望作者用大写字母书写,并且您将 return $this->author 更改为 ucwords( $this->author ),则必须执行此操作的次数你已经复制了那个方法。使用抽象类,您可以定义行为,同时将类本身标记为不完整。这非常方便。
You could, naturally. However, if there are many objects that are of pretty much the same type, it might help to extract common functionality out into a "base" class, which means you don't have to duplicate that logic.
There are actually two reasons though. The first reason, for me, would be that all descendants of your abstract class have the same type, and both adhere to the exact same interface. That means that a PDF document for example will have the same interface as a docx document, and the client code doesn't have to care which object it's handling. Short example (in PHP).
First of all; mind the fact that the DocumentHandler class has no knowledge of which type of Document it's actually handling. It doesn't even care. It's ignorant. However, it does know which methods can be called upon, because the interface between the two types of Documents are the same. This is called polymorphism, and could just as easily be achieved with the implementation of a Document interface.
The second part is; if each and every document has an author, and that author is always requested, you could copy the method over to the PdfDocument as well as the DocxDocument, but you'd be duplicating yourself. For instance, if you decide that you want the author to written with capitals, and you'd change return $this->author to ucwords( $this->author ), you'd have to do it as many times as you've copied that method. Using an abstract class, you can define behaviour, while marking the class itself as incomplete. This comes in very handy.
当您有许多具有相同方法的类时,抽象类会为您提供帮助。
示例:
将会发生什么:
new Foo();
,Foo
是抽象的。Bar.foo1()
和Baz.foo1()
,它们会做同样的事情。Baz
没有实现抽象方法foo2
。它有用的示例:
您希望每个类都能够
display()
,但本身不存在“Shape”这样的东西。Abstract classes help you when you have many classes that have the same methods.
Example:
What will happen:
new Foo();
,Foo
is abstract.Bar.foo1()
andBaz.foo1()
, they will do the same thing.Baz
doesn't implement the abstact methodfoo2
.Example where it is usefull:
You want every class to be able to
display()
, but there is no such thing as "Shape" by itself.并非所有抽象类的方法都必须为空,可能有一些基本方法(和属性)可以使用。例如 - 您有一个电子商店,并且您开发了一个抽象类来导入产品。该类有一个将产品保存到数据库的方法,生成产品的 url 和一个从某处检索产品的抽象方法(因此必须在扩展类中实现)。
接口只有空白方法,没有属性(但可以有常量),因此可能没有实际逻辑,只有方法常量、方法名称及其访问修饰符。
Not all of the abstract class' methods must be empty, there may be some basic methods (and properties) to work with. For example - you have and e-shop and you develop an abstract class to import products. The class has a metod to save products to db, to generate product's url and an abstract method to retrieve products from somewhere (which therefore has to be implemented in the extended class).
Interface only has blank methods and no properties (can have constants though), so tere may be no actual logic, just method constants, method names and their access modifiers.
顾名思义,接口的目的是显式声明类和实例提供的数据和方法的接口,而不需要立即对这些方法进行编码。典型的例子是几何形状。这样的接口可以定义一个产生这种形状的面积的方法:
可能有许多不同的类实现这个接口,例如
Circle
和Square
,它们将提供不同的实现对于方法getArea()
。然后,您可以实现一个显示任何几何形状信息的函数:您可以将实现
Shape
接口的任何对象传递给此函数,并且该接口保证getArea()
方法存在。当然,这些概念在比 PHP 强类型的编程语言中可能更有用。
As the name suggests, the purpose of an interface is to explicitly declare the interface to data and methods provided by a class and the instances, without the need to code those methods right away. The classic example is that of geometric shapes. Such an interface could define a method that produces the area of such a shape:
There could be a number of different classes implementing this interface like
Circle
andSquare
that would provide different implementations for the methodgetArea()
. You could then implement a function that displays information on any geometric shape:You could pass any object implemented the
Shape
interface to this function, and the interface guarantees that thegetArea()
method is present.Of course, these concepts might be more useful in programming languages that are more strongly typed than PHP.