Differences between interfaces and abstract classes-roucheng

Source: Internet
Author: User
Tags define abstract
Differences between interfaces and abstract classes-what are the differences between roucheng interfaces and abstract classes?

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 specific objects. however, the abstract level of the interface is the highest. 2. abstract classes can have specific methods and attributes. interfaces can only have abstract methods and immutable constants. abstract classes are mainly used to abstract classes, and interfaces are mainly used to abstract functions.
4. abstract classes without any implementations. the derived classes must overwrite 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.

Abstract classes have far more functions than interfaces. However, it is costly to define abstract classes. Because in advanced languages (also in actual design), each class can only inherit one class. In this class, you must inherit or compile

All commonalities. Although the interface is much weaker in function, it only describes an action. In addition, you can implement multiple interfaces in a class at the same time. This will reduce the difficulty in the design phase.

Interface usage

Interface: interface

In PHP, we can specify the Public External operations that an object should have, and use interfaces to specify.
The common method is the interface. Specifies which public operation methods (interfaces) an object should be used for. this is also called an interface (a set of public operation methods)
Interface (interface Structure, public method set)

Public method (interface method)
Definition: a structure used to limit the public operation methods required by an object. it is called an interface)
Syntax: defines the interface structure and uses the interface keyword. Some common methods are defined in the interface.

Note:
1. interface method. the access permission must be public.
2. there can only be public methods in the interface, and member variables cannot exist.
3. the interface can only contain unimplemented methods, also called abstract methods, but does not need abstract keywords.

Class implementation interface, using the keyword implements.

In this way, the class that implements this interface must implement all the abstract methods in the interface. It is also certain that this method must be a public external operation method.

Multiple implementations: This function can be theoretically implemented through abstract classes, but the abstract classes are not professional.
The use of interfaces is more professional, because php supports multiple implementations, but only supports single inheritance.

Supports php object interfaces. class constants can be defined, and interfaces can be inherited.

Abstract methods and abstract classes

In OOP, a class can have one or more sub-classes, and each class has at least one public method
External code access its interface. Abstract methods are introduced to facilitate Inheritance. let's take a look at the abstract class and
The definition of the abstract method illustrates its purpose.
What is an abstract method? The method defined in the class without a method body is an abstract method.
The method body means that there is no braces or content in the method declaration, but directly after the method name
End with a semicolon, and add the keyword "abstract" to declare the abstract method;
For example:
Abstract function fun1 ();
Abstract function fun2 ();
In the above example, the abstract methods "fun1 ()" and "fun2 ()" modified by "abstract" without method bodies should not be forgotten.
An abstract method is followed by a semicolon. So what is an abstract class? As long as there is a method in a class that is the abstract side
This class must be defined as an abstract class. abstract classes must also be modified using the "abstract" keyword.
It can have non-abstract methods and member attributes, but as long as there is a method that is abstract, this class must declare
It is an abstract class and can be modified using abstract.

Http://hovertree.com/menu/php/

In the above example, an abstract class "Demo" is defined and "abstract" is used for modification. in this class,
Member attributes "$ test", and two abstract methods "fun1" and "fun2" have a non-abstract method fun3 (); that
How do we use abstract classes? The most important thing is that abstract classes cannot generate instance objects, so they cannot directly
We have mentioned many times that classes cannot be used directly. We use objects instantiated by classes.
The object class cannot generate instance objects. what is the purpose of declaring an abstract class? We use the abstract method as the modulo for subclass overloading.
The abstract class is equivalent to defining a specification, which requires sub-classes to comply with and sub-classes to follow the function abstraction.
Class, the abstract methods in the abstract class are implemented according to the requirements of the subclass. Subclass must include all abstract methods in the parent class
Otherwise, there are still abstract methods in the subclass, so the subclass is still an abstract class and still cannot be instantiated. why?
Do they have to inherit from abstract classes? Sometimes, to implement some functions, we must inherit from the abstract class; otherwise
You cannot implement these functions. if you inherit an abstract class, you must implement the abstract methods in the class;

Singleton mode

  1. Singleton mode (responsibility mode ):
  2. Simply put, an object (before learning the design model, you need to have a better understanding of object-oriented ideas) is only responsible for a specific task;
  3. Singleton class:
  4. 1. the constructor must be marked as private (access control: prevents external code from using the new operator to create objects). a singleton class cannot be instantiated in other classes but can only be instantiated by itself;
  5. 2. static member variables of an instance that stores classes
  6. 3. there is a public static method to access this instance (the getInstance () method is often used to instantiate the singleton class, and the instanceof operator can detect whether the class has been instantiated)
  7. In addition, you must create the _ clone () method to prevent objects from being copied (cloned)
  8. Why use the PHP Singleton mode?
  9. 1. php applications mainly lie in database applications. Therefore, a large number of database operations exist in an application. Using the Singleton mode can avoid the resources consumed by a large number of new operations.
  10. 2. if you need a class in the system to globally control some configuration information, you can easily implement it using the Singleton mode. for details, refer to the FrontController section of ZF.
  11. 3. in a page request, debugging is easy. because all the code (such as database operation db) is concentrated in a class, we can set hooks in the class and output logs, this avoids var_dump and echo everywhere.
  12. Code implementation:
  1. /1 **
  2. * Singleton mode in design mode
  3. * $ _ Instance must be declared as a static private variable
  4. * Constructor and Destructor must be declared as private to prevent external programs from new
  5. * Class, thus losing the significance of Singleton mode
  6. * The getInstance () method must be set to public and must be called
  7. * To return a reference to the instance
  8. *: The operator can only access static variables and static functions.
  9. * New objects consume memory.
  10. * Use cases: database connections are the most common scenarios.
  11. * After an object is generated in Singleton mode,
  12. * This object can be used by many other objects.
  13. */

Class Danli {// Save the static member variable private static $ _ instance of the class instance; // Constructor of the private tag private function _ construct () {echo 'This is a Constructed method; ';} // create the _ clone method to prevent the object from being copied and cloned. public function _ clone () {trigger_error ('Clone is not allow! ', E_USER_ERROR);} // Singleton method, used to access the public static method of 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 ';} // He/He asked hovertree.com//new private /$ danli = new Danli (); // the correct method, with a double colon :: operator to access the static method to obtain the instance $ danli = Danli: getInstance (); $ danli-> test (); // Copy (clone) the object will cause an E_USER_ERROR $ danli_clone = clone $ danli;

Http://www.cnblogs.com/roucheng/p/3528396.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.