Home >Backend Development >PHP Tutorial >The difference between interface and abstract class, the difference between interface abstract class_PHP tutorial

The difference between interface and abstract class, the difference between interface abstract class_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:53:00948browse

The difference between interface and abstract class, the difference between interface abstract class

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 subclasses

All 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".

http://hovertree.com/menu/php/

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;




Single case mode

  1. 单例模式(职责模式):  
  2. 简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务;  
  3. 单例类:  
  4. 1、构造函数需要标记为private(访问控制:防止外部代码使用new操作符创建对象),单例类不能在其他类中实例化,只能被其自身实例化;  
  5. 2、拥有一个保存类的实例的静态成员变量  
  6. 3、拥有一个访问这个实例的公共的静态方法(常用getInstance()方法进行实例化单例类,通过instanceof操作符可以检测到类是否已经被实例化)  
  7. 另外,需要创建__clone()方法防止对象被复制(克隆)  
  8. 为什么要使用PHP单例模式?  
  9. 1、php的应用主要在于数据库应用, 所以一个应用中会存在大量的数据库操作, 使用单例模式, 则可以避免大量的new 操作消耗的资源。  
  10. 2、如果系统中需要有一个类来全局控制某些配置信息, 那么使用单例模式可以很方便的实现. 这个可以参看ZF的FrontController部分。  
  11. 3、在一次页面请求中, 便于进行调试, 因为所有的代码(例如数据库操作类db)都集中在一个类中, 我们可以在类中设置钩子, 输出日志,从而避免到处var_dump, echo。  
  12. 代码实现:
  1. /1**  
  2. * 设计模式之单例模式  
  3. $_instance必须声明为静态的私有变量  
  4. * 构造函数和析构函数必须声明为私有,防止外部程序new  
  5. * 类从而失去单例模式的意义  
  6. * getInstance()方法必须设置为公有的,必须调用此方法  
  7. * 以返回实例的一个引用  
  8. * ::操作符只能访问静态变量和静态函数  
  9. new对象都会消耗内存  
  10. * 使用场景:最常用的地方是数据库连接。   
  11. * 使用单例模式生成一个对象后,  
  12. * 该对象可以被其它众多对象所使用。   
  13. */

 

<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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125681.htmlTechArticle接口与抽象类的区别,接口抽象类区别 接口和抽象类有什么区别 你选择使用接口和抽象类的依据是什么? 接口和抽象类的概念不一样。接...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn