Abstract classes and Interfaces
2014-9-2 9:14:32
Abstract:1, abstract 2,interface (interface)3, polymorphic applications
1. Abstract class
Characteristics:
1) Abstract Keyword Declaration
2) There can be no abstract method in an abstract class
3) Any class, if there is at least one abstract method inside it, then the class must be declared abstract
4) Abstract method without method body, direct semicolon (;) end, using the abstract keyword declaration
5) Abstract methods cannot be instantiated and can only be inherited (extends); Subclasses must overload all abstract methods in the abstract class
Role:
1) Define a specification that is binding, forcing subclasses to define these methods (abstract methods in the parent class)
Attention:
1) When inheriting an abstract class, the subclass must define all the abstract methods in the parent class
2) Access control for these overloaded methods must be the same as in the parent class (or more lenient)
For example, if an abstract method is declared as protected, the methods implemented in the subclass should be declared as protected or public, and cannot be defined as private
3) The method must be called in the same way, i.e. the type and the required number of parameters must be the same
For example, a subclass defines an optional parameter, and the declaration of the parent abstract method does not, and the declaration does not conflict.
Example:
1 Abstract classdemo{2Prviate$param;3 Public Abstract functionFun ();//defines an abstract method that must be overloaded with the4 Public functionTest () {5 //...6 }7 }8 9 classSubdemoextendsdemo{Ten Public functionFun () { One //... A } -}
2. Interface (interface)
Interfaces and abstract classes are very much like, are constrained, but the interface inside the method is all abstract, and do not need to use the abstract Declaration;
Interface use interface declaration
Characteristics:
1) Interface Use interface declaration
2) The methods in the interface are all abstract and do not require the use of the abstract Declaration
3) An interface cannot be instantiated and must be referenced (implements, inherited from an abstract class) using
4) subclasses can refer to multiple interfaces, separated by commas (,)
Role:
1) Define a specification that is binding
2) using interfaces (interface), you can specify which methods a class must implement, but do not need to define the specifics of these methods
Attention:
1) To implement an interface, use the implements operator
2) All methods defined in the interface must be implemented in the class, or a fatal error will be reported
3) classes can implement multiple interfaces, separating the names of multiple interfaces with commas
4) When implementing multiple interfaces, the methods in the interface cannot have duplicate names
5) interfaces can also be inherited by using the extends operator
Example:
1 Interfacedb{2 Public functionSelect ();3 Public functionfindAll ();4 Public functionfind ();5 Public functionInsert ();6 Public functionupdate ();7 Public functionDelete ();8}
3. polymorphic applications
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.
For example, the USB in the life can realize polymorphic function: Insert USB stick and mouse can work properly, and do their work, but there must be a consistent specification definition inside
Example:
1<?PHP2 //define a Perimeter control interface PCI3 Interfacepci{4 //The PCI must have the following features5 Public functioninit ();6 Public functionstop ();7 }8 9 //defining the Motherboard classTen classmainborad{ One Public functionWork (PCI$m){//type constraint, the object must be passed in A $m-init (); - $m-stop (); - } the } - - //Define a sound card reference PCI specification - classSoundcardImplementspci{ + Public functioninit () { - Echo"The sound card is initializing ...<br>"; + } A Public functionStop () { at Echo"sound card is stopping ...<br>"; - } - } - - //defining the NIC Reference PCI specification - classNetworkcardImplementspci{ in Public functioninit () { - Echo"Nic is initializing ...<br>"; to } + Public functionStop () { - Echo"Nic is stopping ...<br>"; the } * } $ Panax Notoginseng $sys=NewMainborad (); - $sys->work (NewSoundcard);//incoming sound card object the $sys->work (NewNetworkcard);//incoming network Card object +?>
PHP Object-oriented 05