Home php教程 php手册 php学习笔记 面向对象中[接口]与[多态性]的应用

php学习笔记 面向对象中[接口]与[多态性]的应用

Jun 13, 2016 pm 12:08 PM
php and code copy polymorphism study object application technology interface yes of notes For

复制代码 代码如下:


/* 接口技术
*
* 接口是一种特殊的抽象类,抽象类又是一种特殊的类
*
* 接口和抽象类是一样的作用
*
* 因为在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
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Services CakePHP Services Sep 10, 2024 pm 05:26 PM

This chapter deals with the information about the authentication process available in CakePHP.

See all articles