


photoshop learning php learning notes application of [interface] and [polymorphism] in object-oriented
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.
