博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql面向对象抽象类和接口
阅读量:5020 次
发布时间:2019-06-12

本文共 3237 字,大约阅读时间需要 10 分钟。

  1. 抽象类:含有抽象方法的类【abstract 抽象关键词】
    1 abstract class Usb 2 { 3     public function load(){ 4         echo "

    Usb is loading

    "; 5 } 6 7 public function start(){ 8 echo "

    Usb is starting

    "; 9 }10 11 # 抽象方法12 abstract public function unload();13 }14 15 # 子类16 class Upan extends Usb17 {18 # 调用需先实现unload抽象方法19 public function unload(){20 echo "

    Usb is unloading

    ";21 }22 23 public function jitui(){24 echo "

    我喜欢鸡腿

    ";25 }26 }27 28 $obj = new Upan();29 $obj->load();

     

  2. 接口:只含有抽象方法的类【interface、abstract 抽象关键词】
    1 interface Usb 2 { 3     public function load(); 4  5     public function start(); 6  7     public function unload(); 8 } 9 10 # 子类11 class Upan implements Usb12 {13     public function load(){14         echo "

    Usb is loading

    ";15 }16 17 public function start(){18 echo "

    Usb is starting

    ";19 }20 21 public function unload(){22 echo "

    Usb is unloading

    ";23 }24 }25 26 # 调用前需全部实现Usb类中的抽象方法27 $obj = new Upan();28 $obj->load();

     

  3. 一个子类实现多个接口
    1 interface Usb 2 { 3     public function load(); 4  5     public function start(); 6  7     public function unload(); 8 } 9 10 interface Usb211 {12     public function run();13 }14 15 # 子类16 class Upan implements Usb,Usb217 {18     public function load(){19         echo "

    Usb is loading

    ";20 }21 22 public function start(){23 echo "

    Usb is starting

    ";24 }25 26 public function unload(){27 echo "

    Usb is unloading

    ";28 }29 30 public function run(){31 echo "

    Usb2 is runing

    ";32 }33 }34     35 $obj = new Upan();36 $obj->load();

     

  4. 接口扩展,只能继承不能实现,一般不常用
    1 interface Usb 2 { 3     public function load(); 4  5     public function start(); 6  7     public function unload(); 8 } 9 10 interface Usb2 extends Usb11 {12     public function run();13 }14 15 # 子类16 class Upan implements Usb217 {18     public function load(){19         echo "

    Usb is loading

    ";20 }21 22 public function start(){23 echo "

    Usb is starting

    ";24 }25 26 public function unload(){27 echo "

    Usb is unloading

    ";28 }29 30 public function run(){31 echo "

    Usb2 is runing

    ";32 }33 }34 35 $obj = new Upan();36 $obj->load();

     

  5. 多态小实例
    1 interface Usb 2 { 3     public function load(); 4  5     public function start(); 6  7     public function unload(); 8 } 9 10 # 子类11 class Upan implements Usb12 {13     public function load(){14         echo "

    Usb is loading

    ";15 }16 17 public function start(){18 echo "

    Usb is starting

    ";19 }20 21 public function unload(){22 echo "

    Usb is unloading

    ";23 }24 25 public function run(){26 echo "

    Usb2 is runing

    ";27 }28 }29 30 # 非法类31 class Udisk32 {33 public function load(){34 echo "

    Usb is loading

    ";35 }36 37 public function start(){38 echo "

    Usb is starting

    ";39 }40 41 public function unload(){42 echo "

    Usb is unloading

    ";43 }44 }45 46 # 只有实现了Usb接口中所有方法才能正常调用47 function useUsb(Usb $o){48 $o->load();49 $o->start();50 $o->unload();51 echo "

    ";52 }53 54 $obj = new Upan();55 $obj2 = new Udisk();56 useUsb($obj);57 useUsb($obj2); #失败

     

转载于:https://www.cnblogs.com/yachyu/p/10707609.html

你可能感兴趣的文章
jva基础网络编程
查看>>
js 正计时和倒计时
查看>>
复合数据类型,英文词频统计
查看>>
you-get帮助使用手册
查看>>
nyoj756_重建二叉树_先序遍历
查看>>
sin()函数的实现
查看>>
图像切割之(一)概述
查看>>
JAVA修饰符类型(public,protected,private,friendly)
查看>>
flex利用webservice上传照片
查看>>
IOS开发之Bug--使用KVC的易错情况
查看>>
python list和tuple
查看>>
基础薄弱的反思
查看>>
ORACLE增删改查以及case when的基本用法
查看>>
[转]oracle10客户端PL/SQL Developer如何连接远程服务器上的oracle数据库
查看>>
HTML5 表单元素和属性
查看>>
SDUTOJ 2498 数据结构实验之图论十一:AOE网上的关键路径
查看>>
使用SpringSocial开发QQ登录
查看>>
好玩的游戏
查看>>
2.6. Statistical Models, Supervised Learning and Function Approximation
查看>>
代码说明call和apply方法的区别 (咱们这方面讲解的少,这样的题有变式,需要举例讲解一下)...
查看>>