Copy codeThe Code is as follows:
<? Php
/* Interface technology
*
* An interface is a special abstract class and an abstract class.
*
* Interfaces and abstract classes play the same role.
*
* Because PHP is a single-inherited class, if an abstract class is used, the subclass can no longer inherit other classes.
*
* If you want to implement some specifications and inherit other classes. An interface is required.
*
* Comparison between interfaces and abstract classes
*
* 1. Objects cannot be created for the same purpose. All objects must be implemented by subclasses.
*
* 2. The interface description is different from the abstract class.
*
* 3. The interface is implemented differently.
*
* 4. All methods in the interface must be abstract methods. Only abstract methods can be declared (abstract modification is not required)
*
* 5. The member attributes in the interface can only declare constants, but cannot declare variables.
*
* 6. All access permissions of the interface members must be public. The minimum permission in the abstract class is protected.
*
* Declared interface: interface name {};
*
* 7. Use a class to implement the interface. Instead of using extends, use the implements keyword.
*
* If the subclass is to override the abstract methods in the parent interface, use implements (implementation), Class -- interface, abstract class -- interface to use implements, and interface -- interface to use extends (inheritance)
*
* Some methods in the interface can be implemented using abstract classes.
* If you want subclass to create objects, you must implement all methods in the interface.
* You can define one interface to inherit another interface.
* A class can implement multiple interfaces (develop subclass according to multiple specifications) and separate multiple interface names with commas (,).
* A class can implement one or more interfaces while inheriting a class.
*
* Implements is used for two purposes:
*
* 1. Multiple Interfaces can be implemented, while extends can inherit only one parent class.
*
* 2. If you do not use the extends word, you can inherit a class, so you can use both of them at the same time.
*
* Polymorphism: polymorphism is one of the three main characteristics of object-oriented
*
* "Polymorphism" is an important feature of object-oriented design. It shows the dynamic binding function, also known as "Polymorphism ). Multi-State functions enable full extension during software development and maintenance ). In fact, the most direct definition of polymorphism is to allow different class objects with inheritance relationships to call member functions with the same name to produce different responses.
*
*
*
*
*
*/
// Declare the interface
Interface Demo {
Const HOST = "localhost ";
Const USER = "admin ";
Function fun1 (); // do not add abstract to the declaration method. The default value is. The permission is public.
Function fun2 ();
}
// Interface inheritance
Interface Demo2 extends Demo {
Function fun3 ();
Function fun4 ();
}
Interface Demo3 {
Function fun5 ();
Function fun6 ();
}
Interface Demo4 {
Function fun7 ();
}
Echo Demo: HOST; // constant in the interface that can be accessed
Class Hello {
Function fun8 (){
}
}
// Subclass must implement all methods in the interface
Class UTest extends Hello implements Demo2, Demo3, Demo4 {// implement multiple interfaces
Function fun1 (){
}
Function fun2 (){
}
Function fun3 (){
}
Function fun4 (){
}
Function fun5 (){
}
Function fun6 (){
}
Function fun7 (){
}
}
/* ------------------- Polymorphism ---------------*/
Interface Test {
Function fun1 ();
Function fun2 ();
}
Class One implements Test {
Function fun1 (){
Echo "aaaaaaaaa ";
}
Function fun2 (){
Echo "bbbbbbbbbbbbbb ";
}
}
Class Two implements Test {
Function fun1 (){
Echo & quot; 11111111 & quot ";
}
Function fun2 (){
Echo & quot; 2222222222 & quot ";
}
}
// The same interface implements the same method, different objects, and different outputs. This is the manifestation and Application of polymorphism.
$ Test = new One;
$ Test-> fun1 (); // output a row
$ Test-> fun2 (); // output a row of B
$ Test = new Two;
$ Test-> fun1 (); // output a row of 1
$ Test-> fun2 (); // output a row of 2
?>
<? Php
/* -------------- Use a multi-state application instance to simulate USB devices ------------------*/
// A USB Interface
Interface USB {
Function mount (); // Method for loading USB
Function work (); // how to work with USB
Function unmount (); // Method for detaching a USB
}
// Define a USB flash drive
Class Upan implements USB {// implement USB Interface
Function mount (){
Echo "the USB flash disk is successfully loaded <br/> ";
}
Function work (){
Echo "U disk started to work <br/> ";
}
Function unmount (){
Echo "the USB flash drive has been uninstalled <br/> ";
}
}
// Define a USB device USB mouse
Class Umouse implements USB {// implement USB Interface
Function mount (){
Echo "USB keyboard loaded <br/> ";
}
Function work (){
Echo "USB keyboard started to work <br/> ";
}
Function unmount (){
Echo "USB keyboard unmounted successfully <br/> ";
}
}
// Define a computer
Class Computer {
// How to use a USB device
Function useUSB ($ usb) {// $ usb parameter indicates which USB device is used
$ Usb-> mount (); // call the device Loading Method
$ Usb-> work (); // call the working method of the device
$ Usb-> unmount (); // call the device uninstall Method
}
}
// Define the category of a computer user
Class PcUser {
// Install USB
Function install (){
// Get a computer first
$ Pc = new Computer;
// Get some USB devices
$ Up = new Upan; // get a USB flash drive
$ Um = new Umouse; // get a USB mouse
// Insert the USB device into the computer and use the USB device method in the computer to call the device to be inserted
$ Pc-> useUSB ($ up); // insert a USB flash drive
$ Pc-> useUSB ($ um); // insert a USB mouse
}
}
// Instantiate a computer user
$ User = new PcUser;
$ User-> install (); // install the device
/* ------------- Output content --------------
USB flash disk mounted successfully
U disk started to work
USB flash disk unmounted
USB keyboard mounted
USB keyboard started to work
USB keyboard uninstalled
-----------------------------------*/
?>
Author: codenamed Aurora
Http://www.cnblogs.com/zizhuyuan/archive/2011/06/16/2082262.html