Interface differs from abstract class, interface abstract class difference _php Tutorial

Source: Internet
Author: User

Interface differs from abstract class, interface abstract class difference


What is the difference between an interface and an abstract class

What is the basis of your choice to use interfaces and abstract classes?


The concepts of interfaces and abstract classes are different. An interface is an abstraction of an action, and an abstract class is an abstraction of the root.

The abstract class represents what this object is. The interface represents what this object can do. For example, men, women, these two classes (if it's a class ...) ), their abstract class is human. Description, they are all human.

People can eat, dogs can eat, you can define "eat" as an interface, and then let these classes to implement it.

Therefore, in high-level languages, a class can inherit only one class (abstract class) (just as people cannot be both biological and non-living), but can implement multiple interfaces (eating interfaces, walking interfaces).

Http://hovertree.com/h/bjaf/to3l3tjm.htm

To summarize a few words:

1. Abstract classes and interfaces cannot be instantiated directly, and if instantiated, the abstract class variable must point to the subclass object that implements all the abstract methods, and the interface variable must point to the class object that implements all the interface methods.

2, abstract class to Quilt class inheritance, interface to be class implementation.

3, interface can only do method declaration, abstract class can do method declaration, can also do method to achieve

4, the variables defined in the interface can only be public static constants, the variables in the abstract class are ordinary variables.

5. Abstract methods in abstract classes must all be implemented by the quilt class, if the subclass can not fully implement the parent class abstract method, then the subclass can only be abstract class. Similarly, when an interface is implemented, if the interface method cannot be fully implemented, then the class can only be an abstract class.

6, abstract method can only declare, can not be implemented, interface is the result of design, abstract class is the result of refactoring

7, abstract class can have no abstract method

8, if there is an abstract method in a class, then this class can only be abstract class

9, abstract methods to be implemented, so can not be static, nor can it be private.

10, the interface can inherit the interface, and can inherit the interface more, but the class can only inherit one root.

1. Abstract classes and interfaces are used to abstract specific objects. However, the abstraction level of the interface is up to 2. Abstract classes can have specific methods and properties,  interfaces can only have abstract methods and immutable constants 3. Abstract classes are primarily used to abstract categories, and interfaces are primarily used to abstract functions.
4. In abstract classes, and without any implementations, derived classes must overwrite them. All methods in an interface must be non-implemented.

When you focus on the nature of a thing, use an abstract class, and when you focus on an operation, use an interface.

Abstract classes are much more powerful than interfaces, but the cost of defining abstract classes is high. Because of the high-level language (which is actually designed), each class can inherit only one class. In this class, you must inherit or write all of its subclasses.

All commonalities. Although the interface is much weaker in functionality, it is only a description of an action. And you can implement multiple interfaces in one class at the same time. In the design phase will reduce the difficulty.

Use of interfaces

Interface: interface

In PHP, we can specify what public external operations an object should have, which can be specified using interface.
The common method is the interface. Used to specify which public operation methods (interfaces) An object should be used for, which is also called an interface (a collection of public action methods)
namely: interface (interface structure, public method collection)

Public methods (interface methods)
Definition: A structure used to qualify an object that must have a common operating method, called an interface (interface)
Syntax: Defines the interface structure, using the interface keyword. The interfaces are defined in a number of public methods.

Attention:
1. Interface methods, access permissions must be public
2. There can be only public methods within the interface, no member variables exist
3. Interfaces can only contain methods that are not implemented, also called abstract methods, but do not use the abstract keyword.

The implements class implements the interface and completes with the keyword.

Thus, the class that implements the interface must implement all the abstract methods within the interface. And it is certain that this method must be a public external operation method.

Multi-Implementation: This function, in theory, can be achieved by abstract classes, but abstract classes, not professional.
The use of interfaces is professional, implementation, because PHP supports a multi-implementation, and only support single inheritance.

PHP Object interface Support, you can define class constants, interfaces can also inherit

Abstract methods and abstract classes

In the OOP language, a class can have one or more subclasses, and each class has at least one public method as
The external code accesses its interface. and abstract method is to facilitate the introduction of inheritance, we first look at the abstract class and
The definition of an abstract method illustrates its purpose.
What is an abstract method? The method that we define in the class without a method is an abstract method, the so-called no-side
The law refers to the absence of curly braces and the contents of the method declaration, but directly after the method name at the time of declaration.
With the end of the semicolon, plus a keyword "abstract" when declaring an abstract method;
For example:
Abstract function fun1 ();
Abstract function fun2 ();
The above example is the abstract method that "abstract" modifies without the method body "fun1 ()" and "fun2 ()", do not forget
There is also a semicolon behind the abstract method, so what is an abstract class? As long as there is one method within a class that is abstract
method, the class is defined as an abstract class, and the abstract class is also modified using the "abstract" keyword; in abstract class
Polygons can have methods and member properties that are not abstract, but as long as one method is an abstract method, the class must declare
For abstract classes, use "abstract" to decorate.

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

The above example defines an abstract class "Demo" that uses "abstract" to modify, in which the class defines a
A member property "$test", and two abstract methods "Fun1" and "fun2" have a non-abstract method fun3 ();
How do we use abstract classes? The most important point is that an abstract class cannot produce an instance object, so it cannot directly make
We have mentioned many times before that classes cannot be used directly, and we use objects that are instantiated by class
The image class cannot produce an instance object. What is the use of abstract classes? We are the modulo that the abstract method is to be overloaded as a subclass
Board, the definition of an abstract class is equivalent to defining a specification that requires subclasses to obey, subclass-Class relay abstraction
class, the abstract method inside the abstract class is implemented according to the needs of the subclass. Subclasses must have all the abstract methods in the parent class
are implemented, otherwise there is an abstract method in the subclass, then the subclass is an abstract class, or it cannot be instantiated. Why am I
Do we have to inherit from the abstract class? Because sometimes we have to implement some functions that must inherit from the abstract class, otherwise
These functions you can not realize, if inherit the abstract class, it is necessary to implement the abstract method of the class;

Single-Case mode

    1. Singleton mode (duty mode):
    2. Simply put, an object (before learning design patterns, you need to compare the idea of object-oriented thinking) is only responsible for a specific task;
    3. Singleton class:
    4. 1, constructor needs to be marked private (access control: Prevent external code from using
    5. 3, There is a public static method that accesses this instance (the common getinstance () method instantiates a singleton class, and the instanceof operator can detect whether the class has been instantiated)
    6. Additionally, you need to create a __clone () method to prevent an object from being copied (cloned)
    7. Why use PHP singleton mode?
    8. 1, PHP application is mainly in the database application, so there will be a large number of database operations in an application, using a singleton mode, you can avoid a lot of new operation of the resources consumed.
    9. 2, if you need a class in the system to control some of the configuration information globally, it is convenient to use singleton mode. This can be found in the Frontcontroller section of ZF.
    10. 3, in a page request, for debugging, because all the code (such as database Operation class DB) is concentrated in a class, we can set the hooks in the class, output the log, so as to avoid var_dump everywhere, echo.
    11. Code implementation:
    1. /1**
    2. * Design pattern of a single case mode
    3. * $_instance must be declared as a static private variable
    4. * Constructors and destructors must be declared private to prevent external program new
    5. * Class to lose the meaning of the singleton pattern
    6. * The getinstance () method must be set to public, this method must be called
    7. * To return a reference to the instance
    8. *:: operator can only access static variables and static functions
    9. * new objects will consume memory
    10. * Usage Scenario: The most common place is the database connection.
    11. * Once an object is generated using singleton mode,
    12. * This object can be used by many other objects.
    13. */

classDanli {//to save a static member variable for a class instancePrivate Static $_instance; //How to construct private tagsPrivate function__construct () {Echo' This is a constructed method; '; }     //To Create a __clone method to prevent an object from being cloned Public function__clone () {Trigger_error(' Clone is not allow! ',E_user_error); }     //A singleton method, a public static method for accessing an instance Public Static functiongetinstance () {if(! (Self::$_instanceinstanceof Self)) { Self::$_instance=NewSelf ; }  returnSelf::$_instance; }      Public functionTest () {Echo' Invoke method succeeded '; }     }     //How to ask hovertree.com//the class with new instantiation of the private tag constructor will be error//$danli = new Danli (); Correct method, use double colon:: operator to access the static method to get the instance$danli= Danli::getinstance (); $danli-test (); //Copying (cloning) an object will result in a e_user_error$danli _clone=Clone $danli;

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

http://www.bkjia.com/PHPjc/1125681.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125681.html techarticle What is the difference between an interface and an abstract class , an interface abstract class, and an abstract class? What is the basis of your choice to use interfaces and abstract classes? The concepts of interfaces and abstract classes are different. The next ...

  • Related Article

    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.