php学习笔记 面向对象中[接口]与[多态性]的应用
复制代码 代码如下:
/* 接口技术
*
* 接口是一种特殊的抽象类,抽象类又是一种特殊的类
*
* 接口和抽象类是一样的作用
*
* 因为在PHP是单继承的,如果使用抽象类,子类实现抽象类就不能再去继承其他的类了
*
* 如果既想实现一些规范,又想继承其他类。就要使用接口。
*
* 接口和抽象类的对比
*
* 1.作用相同,都不能创建对象,都需要子类去实现
*
* 2.接口的声明和抽象类不一样
*
* 3.接口被实现方式不一样
*
* 4.接口中的所有方法必须是抽象方法,只能声明抽象方法(不用使用abstract修饰)
*
* 5.接口中的成员属性,只能声明常量,不能声明变量
*
* 6.接口中的成员访问权限,都必须是public,抽象类中最低的权限protected
*
* 声明接口: interface 接口名{ };
*
* 7.使用一个类去实现接口,不是使用extends,而是使用implements关键字
*
* 如果子类是重写父接口中抽象方法,则使用implements(实现),类--接口,抽象类--接口 使用implements,接口--接口 使用extends(继承)
*
* 可以使用抽象类去实现接口中的部分方法
* 如果想让子类可以创建对象,则必须实现接口中的所有方法
* 可以定义一个接口去继承另一个接口
* 一个类可以去实现多个接口(按多个规范开发子类),使用逗号分隔多个接口名称
* 一个类可以在继承一个类的同时,去实现一个或多个接口
*
* 使用implements的两个目的:
*
* 1.可以实现多个接口,而extends词只能继承一个父类
*
* 2.没有使用extends词,可以去继承一个类,所以两个可以同时使用
*
* 多态:多态是面向对象的三大特性之一
*
* “多态”是面向对象设计的重要特性,它展现了动态绑定(dynamic binding)的功能,也称为“同名异式”(Polymorphism)。多态的功能可让软件在开发和维护时,达到充分的延伸性(extension)。事实上,多态最直接的定义就是让具有继承关系的不同类对象,可以对相同名称的成员函数调用,产生不同的反应效果。
*
*
*
*
*
*/
//声明接口
interface Demo{
const HOST="localhost";
const USER="admin";
function fun1();//声明方法不用加abstract,默认就是。权限是public
function fun2();
}
//接口的继承
interface Demo2 extends Demo {
function fun3();
function fun4();
}
interface Demo3{
function fun5();
function fun6();
}
interface Demo4{
function fun7();
}
echo Demo::HOST;//可以访问接口中的常量
class Hello{
function fun8(){
}
}
//子类必须实现接口中的所有方法
class UTest extends Hello implements Demo2,Demo3,Demo4 { //实现多个接口
function fun1(){
}
function fun2(){
}
function fun3(){
}
function fun4(){
}
function fun5(){
}
function fun6(){
}
function fun7(){
}
}
/*-------------------多态---------------*/
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";
}
}
//同一个接口,实现同一个方法,不同对象,输出不同。这就是多态的表现与应用
$test=new One;
$test->fun1();//输出一行 a
$test->fun2();//输出一行 b
$test=new Two;
$test->fun1();//输出一行 1
$test->fun2();//输出一行 2
?>
/*--------------多态的一个应用实例 模拟USB设备的使用------------------*/
//一个USB的接口
interface USB{
function mount();//装载USB的方法
function work();//USB工作的方法
function unmount();//卸载USB的方法
}
//定义一个USB设备 U盘
class Upan implements USB{//实现USB接口
function mount(){
echo " U盘 装载成功
";
}
function work(){
echo "U盘 开始工作
";
}
function unmount(){
echo "U盘 卸载成功
";
}
}
//定义一个USB设备 USB鼠标
class Umouse implements USB{//实现USB接口
function mount(){
echo " USB键盘 装载成功
";
}
function work(){
echo "USB键盘 开始工作
";
}
function unmount(){
echo "USB键盘 卸载成功
";
}
}
//定义一个电脑类
class Computer{
//使用USB设备的方法
function useUSB ($usb){//$usb参数表示 使用哪种USB设备
$usb->mount();//调用设备的 装载方法
$usb->work();//调用设备的 工作方法
$usb->unmount();//调用设备的卸载方法
}
}
//定义一个电脑的使用者的类
class PcUser{
//安装USB的方法
function install(){
//首先拿来一台电脑
$pc=new Computer;
//拿来一些USB设备
$up=new Upan;//拿来一个U盘
$um=new Umouse;//拿来一个USB鼠标
//把USB设备插入电脑, 使用电脑中使用USB设备的方法 来调用 要插入的设备
$pc->useUSB($up);//插入U盘
$pc->useUSB($um);//插入USB鼠标
}
}
//实例化一个电脑用户
$user=new PcUser;
$user->install();//安装设备
/*-------------输出内容--------------
U盘 装载成功
U盘 开始工作
U盘 卸载成功
USB键盘 装载成功
USB键盘 开始工作
USB键盘 卸载成功
-----------------------------------*/
?>
作者:代号极光
http://www.cnblogs.com/zizhuyuan/archive/2011/06/16/2082262.html

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
