Lớp trừu tượng trong php

Abstraction and interfaces are two very different tools. The are as close as hammers and drills. Abstract classes may have implemented methods, whereas interfaces have no implementation in themselves.

Abstract classes that declare all their methods as abstract are not interfaces with different names. One can implement multiple interfaces, but not extend multiple classes [or abstract classes].

The use of abstraction vs interfaces is problem specific and the choice is made during the design of software, not its implementation. In the same project you may as well offer an interface and a base [probably abstract] class as a reference that implements the interface. Why would you do that?

Let us assume that we want to build a system that calls different services, which in turn have actions. Normally, we could offer a method called execute that accepts the name of the action as a parameter and executes the action.

We want to make sure that classes can actually define their own ways of executing actions. So we create an interface IService that has the execute method. Well, in most of your cases, you will be copying and pasting the exact same code for execute.

We can create a reference implemention for a class named Service and implement the execute method. So, no more copying and pasting for your other classes! But what if you want to extend MySLLi?? You can implement the interface [copy-paste probably], and there you are, again with a service. Abstraction can be included in the class for initialisation code, which cannot be predefined for every class that you will write.

________số 8

Lớp Phiếu vật có thể khai báo các thuộc tính và các phương thức bình thường và không có tính chất chèm đối tượng [Lớp nhật vật không cho phép khởi tạo tham số, chỉ khai báo mà thôi]

  • Bên cạnh đó nó không cho phép tạo thể hiện, nghĩa là sẽ không thể tạo ra các đối tượng thuộc lớp đó. Ví dụ Animal là 1 lớp hiện vật thì không thể khai báo. $animal = new Animal

  • Các phương thức được định nghĩa là trừu tượng trong lớp trừu tượng thì chỉ khai báo tên hàm và không viết nội dung hàm trong đó. Nội dung chia sẻ được viết trong lớp kế thừa khi ghi đè lại phương thức đó

  • Mình có 1 cái ví dụ đơn giản như thế này

    Chủ Đề