The difference between an interface and an abstract class. What is the difference between an interface and an abstract class? what is the basis for using an interface and an abstract class? The concepts of interfaces and abstract classes are different. Differences between interfaces and abstract classes
What is the difference between an interface and an abstract class?
What is the basis for using 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.
The abstract class indicates what this object is. The interface indicates what the object can do. For example, men and women (if they are classes ......), Their abstract class is human. It means they are all people.
People can eat, dogs can also eat, you can define "eat" as an interface, and then let these classes implement it.
Therefore, in advanced languages, a class can only inherit one class (abstract class) (just as humans cannot be both biological and non-biological ), however, multiple interfaces (meal and walking interfaces) can be implemented ).
Http://hovertree.com/h/bjaf/to3l3tjm.htm
To sum up a few words:
1. abstract classes and interfaces cannot be directly instantiated. to instantiate them, the abstract class variables must point to subclass objects that implement all abstract methods, and the interface variables must point to class objects that implement all interface methods.
2. abstract classes must be inherited by quilt classes, and interfaces must be implemented by classes.
3. the interface can only be used for method Declaration. the abstract class can be used for method declaration or method implementation.
4. the variables defined in the interface can only be public static constants. the variables in the abstract class are common variables.
5. the abstract methods in the abstract class must be implemented by all the quilt classes. if the subclass cannot all implement the parent class abstract methods, the subclass can only be an abstract class. Similarly, if an interface cannot be fully implemented, the class can only be an abstract class.
6. abstract methods can only be declared and cannot be implemented. interfaces are design results and abstract classes are reconstruction results.
7. there can be no abstract methods in the abstract class.
8. If an abstract method exists in a class, the class can only be an abstract class.
9. abstract methods must be implemented, so they cannot be static or private.
10. an interface can inherit an interface and inherit multiple interfaces, but a class can only inherit one interface.
1. Abstract classes and interfaces are used to abstract concrete objects. But the interface has the highest level of abstraction
2. Abstract classes can have concrete methods and properties, and interfaces can only have abstract methods and immutable constants
3.Abstract classes are mainly used to abstract categories, and interfaces are mainly used to abstract functions.
4. The abstract class does not contain any implementation, and the derived class must override them. All methods in the interface must be unimplemented.
When you focus on the essence of a thing, use abstract classes; when you focus on an operation, use interfaces.
The functionality of an abstract class goes far beyond an interface, but defining an abstract class is expensive. Because for high-level languages (as well as practical design) each class can only inherit one class. In this class, you must inherit or write all of its subclasses.
All in common. Although the interface will be weakened a lot in functionality, it is only a description of an action. And you can implement multiple interfaces in a class at the same time. The difficulty will be reduced during the design phase.
Use of interfaces
Interface: interface
In PHP, we can specify which public external operations an object should have, which can be specified using an interface.
The public method is the interface. Used to specify which public operation methods (interfaces) an object should be used for. This is also called an interface (collection of public operation methods).
That is: interface (interface structure, public method collection)
Public method (interface method)
Definition: A structure used to limit the common operation methods that an object must have, called an interface
Syntax: Define the interface structure, using the interface keyword. Within the interface are defined some public methods.
note:
1. Interface methods, access permissions must be public public
2. There can only be public methods in the interface, member variables cannot exist
3. The interface can only contain unimplemented methods, also called abstract methods, but without the abstract keyword.
The class implements the interface and is implemented using the keywords implements.
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.
Multi-implementation: This function can be realized in theory through abstract classes, but abstract classes are not professional.
The interface is more professional. In terms of implementation, PHP supports multiple implementations and only supports single inheritance.
PHP object interface support, class constants can be defined, interfaces can also inherit
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
Its interface is accessed by external code. The abstract method is introduced to facilitate inheritance. Let's first look at the abstract class and
The definition of an abstract method illustrates its purpose.
What is an abstract method? The methods that we have defined in a class without a method body are abstract methods.
The body refers to the fact that when the method is declared, there is no brace and its content, but directly after the method name when it is declared
Add a semicolon to the end, and add a keyword "abstract" when declaring an abstract method;
E.g:
abstract function fun1 ();
abstract function fun2 ();
The above example is the abstract methods "fun1 ()" and "fun2 ()" without method body modified by "abstract", don't forget
There should be a semicolon after the abstract method; so what is an abstract class? As long as a method in a class is an abstract method
Method, then this class must be defined as an abstract class, and the abstract class must also be modified using the "abstract" keyword; in the abstract class
Faces can have non-abstract methods and member properties, but as long as there is a method that is abstract, the class must be declared
An abstract class, decorated with "abstract".
http://hovertree.com/menu/php/
In the above example, an abstract class "Demo" is defined and decorated with "abstract". A class is defined in this class
Member properties "$ test", and two abstract methods "fun1" and "fun2" and a non-abstract method fun3 (); then
So how do we use abstract classes? The most important point is that abstract classes cannot generate instance objects, so they cannot be used directly.
Yes, we mentioned earlier that classes cannot be used directly. We use objects that are instantiated through classes, so
Elephant classes can't produce instance objects. What's the use of declaring an abstract class? We are using abstract methods as a model for subclass overloading
Used by the board, defining an abstract class is equivalent to defining a specification. This specification requires subclasses to abide by it.
After the class, implement the abstract methods in the abstract class according to the needs of the subclasses. Subclasses must put all abstract methods in the parent class
All are implemented, otherwise there are abstract methods in the subclass, then the subclass is still an abstract class, and still cannot be instantiated; why do I
Do we have to inherit from abstract classes? Because sometimes we have to inherit from the abstract class if we want to implement some functions, otherwise
You cannot achieve these functions. If you inherit an abstract class, you must implement the abstract methods in the class;
Singleton pattern
Singleton Mode (Responsibility Mode):
Simply put, an object (before you learn design patterns, you need to understand object-oriented thinking) is only responsible for a specific task;
Singleton class:
1. The constructor needs to be marked as private (access control: prevent external code from using the new operator to create objects). Singleton classes cannot be instantiated in other classes, only by their own instances.
Have a static member variable that holds an instance of the class
3, has a public static method to access this instance (usually getInstance () method to instantiate a singleton class, the instanceof operator can detect whether the class has been instantiated)
In addition, the __clone () method needs to be created to prevent objects from being copied (cloned)
Why use the PHP singleton pattern?
1. The application of PHP mainly lies in database applications, so there will be a large number of database operations in an application. Using singleton mode, you can avoid a lot of resources consumed by new operations.
2. If you need a class in the system to control some configuration information globally, then the singleton pattern can be easily implemented. This can be found in the FrontController section of ZF.
3. In a page request, it is easy to debug, because all the code (such as the database operation class db) is concentrated in one class, we can set hooks in the class, output logs, thereby avoiding var_dump, echo everywhere.
Code:
/1**
* Singleton pattern of design pattern
* $ _instance must be declared as a static private variable
* Constructors and destructors must be declared private to prevent external programs new
* Class thus loses the meaning of singleton pattern
* The getInstance () method must be set to public, this method must be called
* To return a reference to the instance
* :: operator can only access static variables and static functions
* new objects consume memory
* Usage scenario: The most commonly used place is database connection.
* After using the singleton pattern to generate an object,
* This object can be used by many other objects.
* /
class Danli {
// Save the static member variables of the class instance
private static $ _instance;
// private tag constructor
private function __construct () {
echo 'This is a Constructed method;';
}
// Create __clone method to prevent objects from being cloned
public function __clone () {
trigger_error ('Clone is not allow!', E_USER_ERROR);
}
// Singleton method, public static method for accessing the instance
public static function getInstance () {
if (! (self :: $ _ instance instanceof self)) {
self :: $ _ instance = new self;
}
return self :: $ _ instance;
}
public function test () {
echo 'Call method succeeded';
}
}
// How to ask hovertree.com
// The class that instantiates private constructor with new will report an error
// $ danli = new Danli ();
// correct method, use double colon :: operator to access static method to get instance
$ danli = Danli :: getInstance ();
$ danli-> test ();
// Copying (cloning) the object will result in an E_USER_ERROR
$ danli_clone = clone $ danli;
http://www.cnblogs.com/roucheng/p/3528396.html