PHP Learning Notes application of [interface] and [polymorphism] in object-oriented _php Foundation

Source: Internet
Author: User
Tags abstract inheritance
Copy Code code as follows:

<?php
/* Interface Technology
*
* Interface is a special kind of abstract class, abstract class is a special class
*
* Interface and abstract class are the same function
*
* Because in PHP is a single inheritance, if you use abstract classes, subclasses can no longer inherit other classes if they implement abstract classes.
*
* If you want to implement some specifications, you want to inherit other classes. Use an interface.
*
* Comparison of interfaces and abstract classes
*
* 1. The same function, can not create objects, all need subclasses to implement
*
* 2. The declaration of an interface is not the same as an abstract class
*
* 3. Interface is implemented differently
*
* 4. All methods in the interface must be abstract methods, only abstract methods can be declared (do not use abstract decoration)
*
* 5. Member properties in interfaces, only constants can be declared, variables cannot be declared
*
* 6. The member access rights in the interface must be public, with the lowest permissions in the abstract class protected
*
* Declaration Interface: Interface interface name {};
*
* 7. Use a class to implement the interface, not using extends, but using the Implements keyword
*
* If the subclass is overriding the abstract method in the parent interface, use the implements (Implementation), class-Interface, abstract class-interface using implements, interface-interface-use extends (inheritance)
*
* You can use abstract classes to implement some of the methods in the interface
* If you want subclasses to be able to create objects, you must implement all the methods in the interface
* Can define an interface to inherit another interface
* A class can implement multiple interfaces (develop subclasses by multiple specifications), separating multiple interface names with commas
* A class can implement one or more interfaces while inheriting a class
*
* Two purposes of using implements:
*
* 1. Multiple interfaces can be implemented, and extends words can inherit only one parent class
*
* 2. No use of extends words, you can inherit a class, so two can be used 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, which shows the function of dynamic binding (binding), also known as "polymorphism" ("duplicate"). Polymorphic functionality allows software to be fully extensible (extension) when it is developed and maintained. In fact, the most direct definition of polymorphism is to allow different classes of objects with inherited relationships to invoke member functions of the same name to produce different effects.
*
*
*
*
*
*/
declaring interfaces
Interface demo{
Const host= "localhost";
Const user= "admin";
function fun1 ()//declaration method without abstract, the default is. Permission is public
function fun2 ();
}
Inheritance of interfaces
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 an interface
Class hello{
function Fun8 () {
}
}
Subclasses must implement all the 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 () {
}
}
/*-------------------polymorphic---------------* *
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 to implement the same method, different objects, output is different. This is the manifestation and application of polymorphism.
$test =new One;
$test->fun1 ()//Output line A
$test->fun2 ()//output line B
$test =new two;
$test->fun1 ()//Output Line 1
$test->fun2 ()//Output Line 2
?>
<?php
/*--------------An application instance of polymorphism to simulate the use of USB devices------------------* *
A USB interface
Interface usb{
function mount ();//Load USB method
function work (); Method of//USB work
function unmount ()//Uninstall USB method
}
Define a USB device U disk
Class Upan implements usb{//implement USB interface
function mount () {
echo "U Disk load Success <br/>";
}
function work () {
echo "U disk start work <br/>";
}
function Unmount () {
echo "U disk uninstall Success <br/>";
}
}
Define a USB device USB mouse
Class Umouse implements usb{//implement USB interface
function mount () {
echo "USB Keyboard mount success <br/>";
}
function work () {
echo "USB keyboard starts work <br/>";
}
function Unmount () {
echo "USB Keyboard Uninstall success <br/>";
}
}
Define a Computer class
Class computer{
Ways to use USB devices
function Useusb ($USB) {//$USB parameter indicates which USB device is used
$usb->mount ();//The Mount method of the calling device
$usb->work ();//Calling the device's working methods
$usb->unmount ();//The Uninstall method of the calling device
}
}
A class that defines the user of a computer
Class pcuser{
How to install USB
function Install () {
First, get a computer.
$PC =new Computer;
Get some USB devices.
$up =new upan;//to bring a U disk.
$um =new umouse;//to get a USB mouse
Plug a USB device into your computer and use a USB device in your computer to call the device you want to insert
$PC->useusb ($up);//Insert U disk
$PC->useusb ($um);//Insert USB mouse
}
}
Instantiate a computer User
$user =new Pcuser;
$user->install ();//Install Device
/*-------------Output--------------
U Disk loaded successfully
U disk starts work
U Disk Uninstall success
USB Keyboard Mount succeeded
USB Keyboard starts working
USB Keyboard Uninstall succeeded
-----------------------------------*/
?>

Author: symbol Aurora
Http://www.cnblogs.com/zizhuyuan/archive/2011/06/16/2082262.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.