What is an interface (interface)?
Interface is the abstraction of methods. If different classes have the same methods, then you should consider using interfaces.
(1) An interface is a behavioral specification and protocol. In fact, it is an agreement and a constraint between classes. (2) C# does not support multiple inheritance, but it leaves this function to the interface for implementation.
(3) The system resource calling methods between classes are different, making communication between them difficult, but the interface can shield the differences between them and enable them to communicate smoothly.
abstract class(abstractclass)?
1. An abstract class provides only a partial implementation of a type. Abstract classes can have instance variables, and one or more 2. Purpose of abstract classes 1) Concrete classes are not used for inheritance. Scott Meyers once pointed out that whenever possible, avoid inheritance from concrete classes. 2) Suppose there are two concrete classes, class A and class B. Class B is a subclass of class A. Then the simplest modification plan is to create an abstract class (or java interface) C, and then let class A and class B becomes a subclass of abstract class C. 3) Abstract classes should have as much common code as possible. To improve code reuse rate. 4) Abstract classes should have as little data as possible. 3. Patterns and principles based on abstract classes 1) Program for abstractions, not for specific programming. 2) Try to use composition (Composition) instead of inheritance to achieve reuse. 3) Use the template method mode
4. When should inheritance and reuse be used? 1) A subclass is a special category of the superclass, not a role of the superclass. That is to say, two relationships must be distinguished: "is-a" and "has-a". 2) There will never be a situation where you need to replace a subclass with another subclass. If the designer is not sure that a class will not become a subclass of another class in the future, the class should not be designed to be a subclass of the superclass. 3) Subclasses have the responsibility to extend the superclass, but not to replace (Override) or cancel (Nullify) the superclass. 4) Inheritance can only be used when it makes sense in taxonomy, not tool class inheritance. Abstract methods are methods that must be implemented. And only in abstract classes. Interfaces and abstract classesA class can inherit multiple interfaces. . .A class can only inherit one abstract class. . .
Animals must have breathing methods. How to breathe is a matter of subclasses.
attributesand behaviors. Generally speaking, interfaces do not consider attributes, only methods, so that subclasses can freely fill in or extend the methods defined by the interface. As the JAVA prince said, the adapter in event is a good one. application. Using a simple example, such as a teacher, we treat it as an abstract class with its own attributes, such as age, education level, teacher number, etc., and teachers are also divided into many types, we can Inherit the teacher class and extend the unique category attributes, while the universal attributes have been directly inherited.
As for the interface~ Let’s take teachers as an example. Teachers have many behaviors. In addition to the same behaviors as ordinary people, there are also career-related behaviors, such as correcting test papers, giving lectures, etc. We define these behaviors as bodyless. Methods, as a collection, are an interface. Teachers Zhang San and Li Si have different behavioral characteristics, so they can expand their behavioral bodies. In this sense, interface focuses on behavior.
In short, in many cases, interfaces can indeed replace abstract classes, if you do not need to deliberately express inheritance on properties.
The interface defines the general specification for implementing a certain service, declaring the required functions and constants, but does not specify how to implement it. The reason why implementation details are not given is because different entities may need to implement public method definitions in different ways. The key is to establish a set of general principles that must be implemented. Only when these principles are met can the interface be said to be implemented.
Class members are not defined in the interface! The definition of class members is completely left to the implementation class.
Inherit more interfaces
<span style="color:#000000"><span style="color:#0000bb"></span></span>
##
<?php interface a{ public function foo (); } interface b{ public function bar (); } //一个接口类可以继承多个接口 interface c extends a , b{ public function baz (); } class d implements c { public function foo (){ } public function bar (){ } public function baz (){ } } ?>
The above is the detailed content of PHP object-oriented (interface) basic concepts and abstract class examples introduction. For more information, please follow other related articles on the PHP Chinese website!