1, abstract class , using the abstract keyword to decorate a class, this class is an abstract class, if you use the abstract keyword to modify a method, this method is an abstract method, if it is an abstract method can not be implemented (that is, abstract methods are declared, cannot be defined).
The style of the abstract class:
Abstract class Name {
member variables;
Abstract Member Method
}
Why design abstract class this technology?
In actual development, we may have a class that is the parent of another class, but it does not need to be instantiated by itself. The main purpose is to allow subclasses to inherit, so that code reuse can be achieved. At the same time conducive to project designers, design classes. For example, the project manager arranges for many people to write a project code, for unified management, the project manager can first write a project of the abstract class, others on the basis of abstract classes to write.
Abstract class animal{public $name; protected $age; Abstract public Function Cry (); Public Function GetName () { return $this->name; }} Class Cat extends animal{public function Cry () { echo "meow meow"; }} $cat 1 = new Cat (), $cat 1->cry (); Echo $cat 1->getname ();
Abstract class Considerations:
(1), an abstract class cannot be instantiated.
(2), abstract class does not necessarily contain abstract methods, that is, abstract classes can have no abstract methods, can have implemented methods.
(3) If there is an abstract method in a class, the class must be declared as an abstract class.
(4), abstract methods have no function body, that is, abstract methods are only declarations, not defined.
(5), if Class A inherits the abstract class B, a class is required to implement all the abstract methods inherited from Class B.
2, interface , interface is a more abstract abstract class, the method in the abstract class can have the method body, all the methods in the interface have no method body. The interface embodies the design idea of multi-state and high-cohesion low-coupling in programming.
(1), instantiation of the interface
/Analog USB Interface interface iusb{public function Start (); Public function Stop ();} Write the camera class, let him implement the interface//1, when a class implements an interface, it is required that the class must implement all methods of this interface class Camera implements iusb{public function Start () { echo "Camera starts to work"; } Public Function Stop () { echo "camera stopped working";} } Cell phone class Phone implements iusb{public function Start () { echo "phone starts working"; } Public Function Stop () { echo "phone stopped working"; }} $camera 1 = new Camera (), $camera 1->start (); $camera 1->stop (); $phone 1 = new Phone (); $phone 1->start (); $phone 1- >stop ();
(2) The basic syntax of the interface, the interface is to give some methods that are not implemented, packaged together, to a class to use, and then according to the specific circumstances of these methods written.
Interface Interface Name {
Property
Method
}
Methods in an interface cannot have method bodies, can only be declared, and cannot be defined.
(3) How to implement the interface
Class name implements interface 1, interface 2{
}
(4) When to use the interface, (4.1) such as the architect set the specification, let other programmers to implement, such as the architect to do a student management system. Defining interfaces
Interface stumanagerinterface{public function addstudent ($stu); $stu As Object public function delstudent ($id); Public Function Updstudengt ($STU);} Give programmer class stumanage{public function Tianjiaxuesheng () { }}
(4.2) More than one class (no inheritance between classes, is a peer) These classes are going to implement a function.
(5) interface-related details knowledge
(5.1) The interface cannot be instantiated;
(5.2) All methods in the interface can not have the subject, can only declare, no definition;
(5.3) A class can implement multiple interfaces, must be separated by commas;
Class name implements interface 1, interface 2{}
(5.4) The interface can have attributes, but must be constant, and is public;
Interface iusb{ Const A = 90;} echo "OK". Isub::a;class B implements iusb{public function aaa () { isub::a }}
(5.5) interface Methods are Public ;
(5.6) an interface cannot inherit another class, but it can inherit from another interface, a class can inherit only one class, and an interface can inherit multiple interfaces. A class implements an interface that must implement the methods of this interface, as well as the methods of all parent interfaces of this interface.
Interface iusb2{public function A (); Interface iusb3{public function B ();} Interface Iusb extends iusb2,iusb3{ const A = n; Public function test ();} Class Class1 implements iusb{public function a () {} public Function B () {} public function test () {}}
3,final
4,const
PHP Object-Oriented programming _2