Copy code The code is as follows:
/* Interface technology
*
* Interface is a special abstract class, and abstract class is a special class
*
* Interfaces and abstract classes have the same function
*
* Because PHP has single inheritance, if you use an abstract class, the subclass that implements the abstract class cannot inherit other classes
*
* If you want to implement some specifications and inherit other classes. Just use the interface.
*
* Comparison of interfaces and abstract classes
*
* 1. They have the same function, neither can create objects, and both require subclasses to implement them
*
* 2. Interfaces The declaration is different from the abstract class
*
* 3. The interface is implemented differently
*
* 4. All methods in the interface must be abstract methods, and only abstract methods can be declared (no need to use abstract modification)
*
* 5. Member attributes in the interface can only declare constants, not variables
*
* 6. Member access rights in the interface must be public and abstract The lowest permissions in the class protected
*
* Declare the interface: interface interface name { };
*
* 7. Use a class to implement the interface, instead of using extends, use the implements keyword
*
* If the subclass overrides the abstract method in the parent interface, use implements (implementation), class--interface, abstract class--interface uses implements, interface--interface uses extends (inheritance)
*
* You can use abstract classes to implement some methods in the interface
* If you want subclasses to create objects, you must implement all methods in the interface
* You can define an interface to inherit another An interface
* A class can implement multiple interfaces (to develop subclasses according to multiple specifications). Use commas to separate multiple interface names
* A class can inherit one class and implement one or more interfaces at the same time. interface
*
* Two purposes of using implements:
*
* 1. Multiple interfaces can be implemented, while the extends word can only inherit one parent class
*
* 2. You can inherit a class without using the extends word, so both can be used at the same time
*
* Polymorphism: Polymorphism is one of the three major features of object-oriented
*
* " "Polymorphism" is an important feature of object-oriented design. It exhibits the function of dynamic binding, also known as "Polymorphism". Polymorphic functions allow software to achieve full extension during development and maintenance. In fact, the most direct definition of polymorphism is to allow objects of different classes with inheritance relationships to call member functions with the same name and produce different reactions.
*
*
*
*
*
*/
//Declaration interface
interface Demo{
const HOST="localhost";
const USER="admin";
function fun1();//There is no need to add abstract when declaring a method, it is the default. Permission is public
function fun2();
}
//Inheritance of interface
interface Demo2 extends Demo {
function fun3();
function fun4();
}
interface Demo3{
function fun5();
function fun6();
}
interface Demo4{
function fun7();
}
echo Demo ::HOST;//Can access constants in the interface
class Hello{
function fun8(){
}
}
//Subclasses 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 "bbbbbbbbbbbb";
}
}
class Two implements Test{
function fun1(){
echo "11111111" ;
}
function fun2(){
echo "2222222222";
}
}
//The same interface implements the same method, different objects, and the output is different.This is the performance and application of polymorphism
$test=new One;
$test->fun1();//Output a line a
$test->fun2();//Output a line b
$test=new Two;
$test->fun1();//Output a line of 1
$test->fun2();//Output a line of 2
?>
/*--------------A polymorphic application example simulates the use of USB devices------------- -----*/
//A USB interface
interface USB{
function mount();//How to load USB
function work();//How USB works
function unmount();//Method to unmount USB
}
//Define a USB device U disk
class Upan implements USB{//Implement USB interface
function mount(){
echo "The USB disk is loaded successfully
";
}
function work(){
echo "The USB disk starts working
";
}
function unmount(){
echo "U disk unmounted successfully
";
}
}
//Define a USB device USB mouse
class Umouse implements USB{//Implement USB interface
function mount(){
echo " USB keyboard loaded successfully
";
}
function work(){
echo "USB The keyboard starts working
";
}
function unmount(){
echo "USB keyboard uninstalled successfully
";
}
}
//Define a computer class
class Computer{
//Method to use USB device
function useUSB ($usb){//The $usb parameter indicates which USB device to use
$usb- >mount();//Calling the mounting method of the device
$usb->work();//Calling the working method of the device
$usb->unmount();//Calling the unmounting of the device Method
}
}
//Define a computer user class
class PcUser{
//Method to install USB
function install(){
// First get a computer
$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 method of using USB devices in the computer to call the device to be inserted
$pc->useUSB($up);//Insert U Disk
$pc->useUSB($um);//Insert USB mouse
}
}
//Instantiate a computer user
$user=new PcUser;
$user->install();//Install device
/*-------------Output content--------------
U disk loaded successfully
U disk started working
U disk uninstalled successfully
USB keyboard loaded successfully
USB keyboard started working
USB keyboard uninstalled successfully
-------- --------------------------*/
?>
Author: Codename Aurora
http://www.cnblogs.com/zizhuyuan/archive/2011/06/16/2082262.html
http://www.bkjia.com/PHPjc/323677.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323677.htmlTechArticleCopy the code as follows: ?php /* Interface technology* * Interface is a special abstract class. Abstract class It is another special class* * Interface and abstract class have the same function* * Because in PHP it is single...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn