Home  >  Article  >  Backend Development  >  Application of [Interface] and [Polymorphism] in PHP object-oriented

Application of [Interface] and [Polymorphism] in PHP object-oriented

高洛峰
高洛峰Original
2016-11-30 09:54:07780browse

The code is as follows:
/* Interface technology
*
* Interface is a special abstract class, and abstract class is a special class
*
* Interface and abstract class have the same function
*
* Because there is single inheritance in PHP, if you use abstract classes, 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
*
* 2. The declaration of interfaces is different from that of abstract classes
*
* 3. Interfaces are implemented in different ways
*
* 4. All methods in the interface must be abstract methods, and only abstract methods can be declared (without using abstract modification)
*
* 5. Member attributes in the interface can only be declared constants. Variables cannot be declared
*
* 6. The access permissions of members in the interface must be public, and the lowest permission in the abstract class is 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 is to override the abstract method in the parent interface, use implements (implementation), class-interface, abstract class-interface use 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 interface
* A class can implement multiple interfaces (developing 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
*
* Two purposes of using implements:
*
* 1. Multiple interfaces can be implemented, but the extends word can only inherit one parent class
*
* 2. Without using the extends word, you can inherit a class, so Two 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, which shows the power of dynamic binding Function, also known as "polymorphism". Polymorphic functions allow software to achieve full extensibility during development and maintenance. In fact, the most direct definition of polymorphism is to allow objects of different classes with inheritance relationships to produce different responses to member function calls with the same name.
*
*
*
*
*
*/
//Declaration interface
interface Demo{
const HOST="localhost";
const USER="admin";
function fun1();//No need to add the declaration method abstract, the default is. 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;//You can access the constants in the interface
class Hello{
function fun8(){
}
}
//Sub The class 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 "bbbbbbbbbb";
}
}
class Two implements Test{
function fun1(){
echo "11111111";
}
function fun2(){
echo "2222222222";
}
}
//The same interface implements the same method, different objects, and different outputs. 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 mount USB
function work();//How USB works
function unmount();//How to uninstall USB
}
//Define a USB device U disk
class Upan implements USB{//Implement USB interface
function mount() {
echo " The U disk is mounted successfully
";
}
function work(){
echo "The U disk starts working
";
}
function unmount(){
echo " U disk uninstalled successfully
";
}
}
//Define a USB device USB mouse
class Umouse implements USB{//Implement USB interface
function mount(){
echo " USB keyboard loaded successfully< ;br/>";
}
function work(){
echo "USB keyboard starts working
";
}
function unmount(){
echo "USB keyboard uninstalled successfully
}
}
//Define a computer class
class Computer{
//How to use USB devices
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 method of the device
}
}
//Define a computer user class
class PcUser{
//How to install USB
function install(){
//First get a computer
$pc=new Computer;
//Get some USB device
$up=new Upan;//Bring a U disk
$um=new Umouse;//Bring a USB mouse
//Insert the USB device into the computer, and use the method of using the USB device in the computer to call the request Inserted device
$pc->useUSB($up);//Insert U disk
$pc->useUSB($um);//Insert USB mouse
}
}
//Instantiate a computer user
$user=new PcUser;
$user->install();//Install the 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
----------------- ------------------*/
?>

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