Photoshop Learning PHP Learning Notes Object-oriented [interface] and [polymorphism] applications

Source: Internet
Author: User
Copy CodeThe code is as follows:


/* Interface Technology
*
* Interface is a special kind of abstract class, abstract class is also a special class
*
* Interfaces and abstract classes are the same function
*
* Because PHP is single-inheritance, if you use abstract classes, subclasses can no longer inherit other classes by implementing abstract classes.
*
* If you want to implement some specifications, you want to inherit other classes. The interface must be used.
*
* Comparison of interfaces and abstract classes
*
* 1. The same function, can not create objects, all need subclasses to implement
*
* 2. The declaration of the interface is not the same as the abstract class
*
* 3. Interface is not implemented in the same way
*
* 4. All methods in an interface must be abstract methods, only abstract methods can be declared (without using the abstract adornment)
*
* 5. member properties in an interface, only constants can be declared, variables cannot be declared
*
* 6. The member access rights in the interface must be public, 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, then use implements (Implementation), Class--interface, abstract class--interface using implements, interface--interface using extends (inheritance)
*
* You can use abstract classes to implement some of the methods in an interface
* If you want subclasses to be able to create objects, you must implement all the methods in the interface
* You can define an interface to inherit another interface
* A class can implement multiple interfaces (developing 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, whereas extends words only inherit one parent class
*
* 2. You can inherit a class without using the extends word, so you can use both
*
* Polymorphism: Polymorphism is one of the three main characteristics of object-oriented
*
* Polymorphic is an important feature of object-oriented design, which shows the function of dynamic binding, also known as "polymorphism". Polymorphic functionality allows the software to achieve full extensibility (extension) when developing and maintaining. In fact, the most straightforward definition of polymorphism is to allow different classes of objects with inherited relationships to have different reaction effects on member function calls of the same name.
*
*
*
*
*
*/
declaring interfaces
Interface demo{
Const host= "localhost";
Const user= "admin";
function fun1 ();//Declaration method does not add abstract, the default is. Permissions are 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 the 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 implements test{
function Fun1 () {
echo "11111111";
}
function fun2 () {
echo "2222222222";
}
}
The same interface, the implementation of the same method, different objects, the 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;
$test->fun1 ();//Output Line 1
$test->fun2 ();//Output Line 2
?>
/* One application instance--------------polymorphic simulates the use of USB devices------------------*/
A USB interface
Interface usb{
function mount ();//method of loading USB
function work (); Methods of//usb
function unmount ();//method of uninstalling USB
}
Define a USB device drive
Class Upan implements usb{//implements USB interface
function mount () {
echo "USB stick loaded successfully
";
}
function work () {
echo "U disk starts working
";
}
function Unmount () {
echo "U disk Uninstall succeeded
";
}
}
Define a USB device USB mouse
Class Umouse implements usb{//implements USB interface
function mount () {
echo "USB Keyboard loaded successfully
";
}
function work () {
echo "USB Keyboard starts working
";
}
function Unmount () {
echo "USB Keyboard Uninstall succeeded
";
}
}
Define a Computer class
Class computer{
How to use a USB device
function Useusb ($USB) {//$USB parameter indicates which USB device to use
$usb->mount ();//The loading method of the calling device
$usb->work ();//Invoking the device's working methods
$usb->unmount ();//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;//get a USB flash drive.
$um =new umouse;//get a USB mouse
Plug the USB device into your computer and use the USB device in your computer to call the device you want to insert
$PC->useusb ($up);//Insert USB stick
$PC->useusb ($um);//Insert USB mouse
}
}
Instantiate a user of a computer
$user =new Pcuser;
$user->install ();//Installation Equipment
/*-------------Output content--------------
USB Stick loaded successfully
U disk start to work
USB Stick Uninstall succeeded
USB Keyboard loaded successfully
USB Keyboard starts working
USB Keyboard Uninstall succeeded
-----------------------------------*/
?>


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

The above describes the Photoshop learning PHP Learning Notes Object-oriented [interface] and [polymorphism], including the content of Photoshop learning, I hope to be interested in PHP tutorial friends helpful.

  • 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.