Home >Backend Development >PHP Tutorial >The difference between interface and abstract class, the difference between interface abstract class_PHP tutorial
What is the difference between interface and abstract class
What is your basis for choosing to use interfaces and abstract classes?
The concepts of interfaces and abstract classes are different. The interface is the abstraction of actions, and the abstract class is the abstraction of the source.
Abstract class represents what this object is. The interface represents what this object can do. For example, men and women, these two classes (if they are classes...), their abstract class is people. Explain that they are all human beings.
People can eat, and dogs can also eat. You can define "eating" as an interface, and then let these classes implement it.
So, in high-level languages, a class can only inherit one class (abstract class) (just as a person cannot be a living thing and a non-living thing at the same time), but it can implement multiple interfaces (eating interface, walking interface).
http://hovertree.com/h/bjaf/to3l3tjm.htm
To summarize in a few words:
1. Neither abstract classes nor interfaces can be instantiated directly. If they are to be instantiated, abstract class variables must point to subclass objects that implement all abstract methods, and interface variables must point to class objects that implement all interface methods.
2. Abstract classes must be inherited by subclasses, and interfaces must be implemented by classes.
3. Interfaces can only be used for method declarations, while abstract classes can be used for method declarations and method implementations
4. Variables defined in the interface can only be public static constants, and variables in abstract classes are ordinary variables.
5. All abstract methods in an abstract class must be implemented by the subclass. If the subclass cannot implement all the abstract methods of the parent class, then the subclass can only be an abstract class. Similarly, when a class implements an interface, if it cannot implement all the interface methods, then the class can only be an abstract class.
6. Abstract methods can only be declared, not implemented. Interfaces are the result of design, and abstract classes are the result of reconstruction
7. There can be no abstract method in an abstract class
8. If there is an abstract method in a class, then this class can only be an abstract class
9. Abstract methods must be implemented, so they cannot be static or private.
10. Interfaces can inherit interfaces and multiple interfaces, but classes can only inherit from a single root.
1.抽象类 和 接口 都是用来抽象具体对象的. 但是接口的抽象级别最高 2.抽象类可以有具体的方法 和属性, 接口只能有抽象方法和不可变常量 3.抽象类主要用来抽象类别,接口主要用来抽象功能.
4、抽象类中,且不包含任何实现,派生类必须覆盖它们。接口中所有方法都必须是未实现的。
When you focus on the essence of a thing, use abstract classes; when you focus on an operation, use interfaces.
Abstract classes have far more functions than interfaces, but the cost of defining abstract classes is high. Because in high-level languages (and in terms of actual design), each class can only inherit one class. In this class, you must inherit or write out
for all its subclassesAll commonalities. Although the interface will be much weaker in function, it is only a description of an action. And you can implement multiple interfaces in one class at the same time. The difficulty will be reduced during the design stage.
Usage of interface
Interface: interface
In PHP, we can specify what public external operations an object should have, which can be specified using interface.
Public methods are interfaces. Used to specify which public operation methods (interfaces) an object should be used for. This is also called an interface (a collection of public operation methods)
that is: interface (interface structure, collection of public methods)
Public methods (interface methods)
Definition: A structure used to limit the public operation methods that an object must have, called an interface (interface)
Syntax: To define an interface structure, use interface Keywords. What are defined in the interface are some public methods.
Note:
1. Interface methods, access rights must be public public
2. There can only be public methods in the interface, and member variables cannot exist
3. Within the interface It can only contain unimplemented methods, also called abstract methods, but does not use the abstract keyword.
The class implements the interface and uses the keyword implements to complete.
In this way, the class that implements the interface must implement all abstract methods in the interface. And it is certain that this method must be a public external operation method.
Multiple implementations: This function can theoretically be implemented through abstract classes, but abstract classes are not professional.
Using interfaces is more professional in terms of implementation, because PHP supports multiple implementations and only supports single inheritance.
PHP object interface support, class constants can be defined, and interfaces can also be inherited
Abstract methods and abstract classes
In OOP language, a class can have one or more subclasses, and each class has at least one public method as an interface for
external code to access it. Abstract methods are introduced to facilitate inheritance. Let’s first take a look at the definitions of abstract classes and
abstract methods before explaining their uses.
What is an abstract method? The method we define in the class without a method body is an abstract method. The so-called method body means that when the method is declared, there are no curly brackets and its contents, but it is directly included in the method name when declaring it. After
, add a semicolon to end. In addition, when declaring an abstract method, add a keyword "abstract" to modify it;
For example:
abstract function fun1();
abstract function fun2() ;
The above example is the abstract method "fun1()" and "fun2()" without a method body modified by "abstract". Don't forget
There is also a semicolon after the abstract method; so what is abstraction? What about classes? As long as there is an abstract method in a class
, then the class must be defined as an abstract class, and the abstract class must also be modified with the "abstract" keyword; in an abstract class,
can be abstract or not. Methods and member attributes, but as long as one method is an abstract method, the class must be declared
as an abstract class and decorated with "abstract".
In the above example, an abstract class "Demo" is defined and modified with "abstract". In this class, a
member attribute "$test" and two abstract methods "fun1" and " "fun2" also has a non-abstract method fun3(); then
how do we use abstract classes? The most important point is that abstract classes cannot produce instance objects, so they cannot be used directly. We have mentioned many times that classes cannot be used directly. We are using objects instantiated through classes, so abstract classes cannot be used directly. >Image classes cannot produce instance objects. What is the use of declaring abstract classes? We use abstract methods as templates for subclass overloading
. Defining an abstract class is equivalent to defining a specification. This specification requires subclasses to comply. Subclasses inherit abstraction
After the class, implement the abstract methods in the abstract class according to the needs of the subclass. The subclass must implement all abstract methods in the parent class. Otherwise, if there are still abstract methods in the subclass, the subclass will still be an abstract class and cannot be instantiated. Why do we have to start from the abstract class? What about inheritance? Because sometimes we must inherit from an abstract class to implement some functions, otherwise
you will not be able to implement these functions. If you inherit an abstract class, you must implement the abstract method in the class;
<span>class</span><span> Danli { </span><span>//</span><span>保存类实例的静态成员变量 </span> <span>private</span> <span>static</span> <span>$_instance</span><span>; </span><span>//</span><span>private标记的构造方法 </span> <span>private</span> <span>function</span><span> __construct(){ </span><span>echo</span> 'This is a Constructed method;'<span>; } </span><span>//</span><span>创建__clone方法防止对象被复制克隆 </span> <span>public</span> <span>function</span><span> __clone(){ </span><span>trigger_error</span>('Clone is not allow!',<span>E_USER_ERROR</span><span>); } </span><span>//</span><span>单例方法,用于访问实例的公共的静态方法 </span> <span>public</span> <span>static</span> <span>function</span><span> getInstance(){ </span><span>if</span>(!(self::<span>$_instance</span><span> instanceof self)){ self</span>::<span>$_instance</span> = <span>new</span><span> self; } </span><span>return</span> self::<span>$_instance</span><span>; } </span><span>public</span> <span>function</span><span> test(){ </span><span>echo</span> '调用方法成功'<span>; } } </span><span>//</span><span> 何问起 hovertree.com //用new实例化private标记构造函数的类会报错 //$danli = new Danli(); //正确方法,用双冒号::操作符访问静态方法获取实例 </span> <span>$danli</span> = Danli::<span>getInstance(); </span><span>$danli</span>-><span>test(); </span><span>//</span><span>复制(克隆)对象将导致一个E_USER_ERROR </span> <span>$danli_clone</span> = <span>clone</span> <span>$danli</span>;
http://www.cnblogs.com/roucheng/p/3528396.html