I. 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 that accesses its interface as an external code. Abstract methods are introduced to facilitate inheritance.
Abstract method: When there is a method in the class, he has no method body, that is, no curly braces, the direct semicolon ends , like this method we call abstract method, must use the keyword abstract definition .
For example: Public abstract function fun ();
Abstract class: A class that contains an abstract method must be an abstract class and be declared with the keyword abstract. (That is, classes that use the keyword abstract adornment are abstract classes )
Features of abstract classes:
Cannot be instantiated, also new object
To use an abstract class, you must define a class to inherit the abstract class and define an abstract method that overrides the parent class (implementing an abstract method).
In fact, abstract classes for subclasses (Implementation classes), there is a binding role. Subclasses inherit an abstract parent class must implement all the abstract methods in the parent class (unless it is also an abstract class)
Note: Classes that contain abstract methods are definitely abstract classes, but not all abstract classes must contain abstract methods.
1<?PHP2 //Abstract class parent Class A3 Abstract classA4 {5 Public functionfun1 ()6 {7 Echo"Fun1 () ...<br/>";8 }9 Ten Public functionfun2 () One { A Echo"Fun2 () ...<br/>"; - } - the Public Abstract functionFun3 (); - - /** - * Summation Method + * @param int $a is summed parameter 1 - * @param int $b is summed parameter 2 + * @return int returns the sum result A */ at Public Abstract functionAdd$a,$b); - - } - - classBextendsA - { in Public functionfun3 () - { to Echo"Fun3 () ...<br/>"; + } - the Public functionAdd$x,$y)//This is the constraint: The function name must be add, and the argument must have two * { $ return $x+$y;Panax Notoginseng } - } the + $b=NewB (); A $b-fun1 (); the $b-fun2 (); + $b-fun3 (); - Echo $b->add (10,20); $?>
Second, the interface
PHP, like most object-oriented programming languages, does not support multiple inheritance, meaning that each class can inherit only one parent class . To solve this problem, PHP introduced the interface, the idea of the interface is to specify a implementation of the interface of the class must implement a series of functions.
Interface: If you include only abstract methods in an abstract class, you can define it as interface (interface) and use it with the implements (Implementation) keyword.
define the format :
Interface Interface Name
{
Constant members (defined with the const keyword)
Abstract method (no need to use the abstract keyword)
}
Use format : Class name implements Interface Name 1, interface name 2{...}
When a class implements an interface, it must implement all methods that overwrite the interface before it can be instantiated, otherwise it is an abstract class.
1<?PHP2 //Interface3 InterfaceA4 {5 Public functionfun1 ();6 7 Public functionfun2 ();8 9 Public functionfun3 ();Ten One } A - //defines the Class B implementation interface a - Abstract classBImplementsA the { - Public functionfun1 () - { - Echo"Fun1 () ...<br/>"; + } - Public functionfun2 () + { A Echo"Fun2 () ...<br/>"; at } - - } - classCextendsB - { - Public functionfun3 () in { - Echo"Fun3 () ...<br/>"; to } + } - the $b=NewC (); * $b-fun1 (); $ $b-fun2 ();Panax Notoginseng $b-fun3 (); -?>
Three, the difference between abstract class and interface
Defined:
Abstract classes represent specific definitions of methods that may already exist in the class.
Interfaces can only define the interface of each method, and there can be no specific implementation code in the member method.
Usage:
Abstract classes are inherited by subclasses and can be implemented in subclasses when the parent class already has a method that is actually functional.
To implement an interface, you must implement all the defined methods in the interface, and you cannot omit any one.
Understanding: The interface is like the company boss (set a series of goals each year, do not do, only propose a variety of plans)
Abstract classes are like managers at all levels of the company (with a plan set by the boss, to achieve part, to distribute part of the task staff)
The class that ultimately inherits is the ordinary employee (receives the task that the manager distributes, according to the request, completes without omission)
Iv. polymorphic
The polymorphism of an object refers to a property or behavior defined in a parent class that, after the quilt class inherits, can have different data types or behave differently. This makes the same attribute or behavior have different semantics in the parent class and its subclasses.
Why polymorphic?
Polymorphism can make programs more extensible and reduce the amount of code changes.
The method of implementing polymorphism : Inheritance (rewrite), abstraction, interface .
For example, the drawing method for geometry, ellipse and polygon are subclasses of geometry, and the drawing method functions differently.
1<?PHP2 // polymorphic3 //PCI slot interface on the motherboard4 Abstract classPCI5 {6 Public Abstract functionstart ();7 Public Abstract functionstop ();8 }9 Ten //Motherboard class One classmainboard A { - Public functionRunning (PCI$pci) - { the $pci-start (); - $pci-stop (); - } - } + - //sound card manufacturers + classSoundcardextendsPCI A { at Public functionStart () - { - Echo"Sound card starts ...<br/>"; - } - - Public functionStop () in { - Echo"Sound card Stop ...<br/>"; to } + } - the //Network card Manufacturers * classNetworkcardextendsPCI $ {Panax Notoginseng Public functionStart () - { the Echo"Nic Boot ...<br/>"; + } A the Public functionStop () + { - Echo"Nic Stop ...<br/>"; $ } $ } - - the //use: Main program - $MB=Newmainboard ();Wuyi $SC=Newsoundcard (); the $NC=NewNetworkcard (); - Wu $MB->running ($SC); - $MB->running ($NC); About?>
PHP Object-oriented (v)