Home >Backend Development >PHP Tutorial >photoshop learning php learning notes application of [interface] and [polymorphism] in object-oriented

photoshop learning php learning notes application of [interface] and [polymorphism] in object-oriented

WBOY
WBOYOriginal
2016-07-29 08:45:361018browse

Copy the code 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 In PHP, there is single inheritance. 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 them
*
* 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 as constants. Variables cannot be declared
*
* 6. Access rights to 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 overrides 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 demonstrates the power of dynamic binding Function, 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 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();
}
//Inheritance of interface
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 "bbbbbbbbbbbb";
}
}
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();//Method to mount USB
function work();//Method to work USB
function unmount();//Method to unmount USB
}
//Define a USB device U disk
class Upan implements USB{//Implement the USB interface
function mount(){
echo "The U disk was loaded successfully
";
}
function work(){
echo "The U disk started 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 mounted successfully
";
}
function work(){
echo "USB keyboard started 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( );//Call the uninstall method of the device
}
}
//Define a computer user class
class PcUser{
//Method to install USB
function install(){
//First get a computer
$pc=new Computer;
//Bring some USB devices
$up=new Upan;//Bring a USB flash drive
$um=new Umouse;//Bring a USB mouse
//Insert the USB device Computer, use the method of using USB devices in the computer to call the device to be inserted
$pc->useUSB($up);//Insert the U disk
$pc->useUSB($um);//Insert the USB mouse
}
}
//Instantiate a computer user
$user=new PcUser;
$user->install();//Install the device
/*-------------Output Contents--------------
The U disk was loaded successfully
The U disk started working
The U disk was uninstalled successfully
The USB keyboard was loaded successfully
The USB keyboard started working
The USB keyboard was uninstalled successfully
---- ----------------------------------*/
?>


Author: Codename Aurora
http://www .cnblogs.com/zizhuyuan/archive/2011/06/16/2082262.html

The above introduces the application of [Interface] and [Polymorphism] in object-oriented learning of Photoshop and PHP learning notes, including the content of Photoshop learning. I hope it will be helpful to friends who are interested in PHP tutorials.

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