Blogger Information
Blog 19
fans 0
comment 0
visits 16172
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php基础接口的使用、trait的使用
῀℡῀ᵒᵐᵍᵎᵎᵎ
Original
1090 people have browsed it

接口的使用

代码演示

  1. <?php
  2. //多接口继承
  3. interface iFater
  4. {
  5. const RMB = '100' ;
  6. }
  7. interface iSon1 extends iFater
  8. {
  9. const NA_RMB = '50';
  10. }
  11. interface iSon2 extends iFater, iSon1
  12. {
  13. public function hua();
  14. }
  15. //实现类
  16. // class Boss implements iFater, iSon1, iSon2
  17. // {
  18. // public function hua()
  19. // {
  20. // return '爸爸拿了'. iFater::RMB.'元给大儿子,大儿子给了弟弟'. iSon1::NA_RMB ;
  21. // }
  22. // }
  23. //简化代码
  24. class Boss implements iSon2
  25. {
  26. public function hua()
  27. {
  28. return '爸爸拿了'. iFater::RMB.'元给大儿子,大儿子给了弟弟'. iSon1::NA_RMB ;
  29. }
  30. }
  31. echo (new Boss) -> hua();

演示截图

代码演示二

  1. <?php
  2. //mp3接口
  3. interface iMp3
  4. {
  5. const MUISCC = '听音乐';
  6. }
  7. //mp4接口
  8. interface iMp4 extends iMp3
  9. {
  10. const MOVIE = '看电影';
  11. }
  12. //手机接口
  13. interface IShouji extends iMp4
  14. {
  15. public function tel();
  16. }
  17. //手机可以打电话听音乐
  18. class Mobile implements iShouji
  19. {
  20. public $tel='打电话';
  21. public function tel()
  22. {
  23. echo '手机不紧可以'.$this->tel.'还可以'.iMp4::MOVIE.'更可以'.iMp3::MUISCC;
  24. }
  25. }
  26. $obj = new Mobile();
  27. $obj->tel();

演示截图


trait的简单用法

代码演示

  1. <?php
  2. //trait 功能与语法
  3. //trait:代码复用。只限于php5.4以上
  4. //trait:与抽象类,接口一样不能实例化,只能嵌入到宿主类中使用
  5. //triat是一个特殊类
  6. trait myTrait {
  7. public $myName = 'misszhou';
  8. public function fly() {
  9. echo '我能飞';
  10. }
  11. public function run() {
  12. echo '我能跑';
  13. }
  14. }
  15. class person {
  16. public function eat() {
  17. echo '我能吃';
  18. }
  19. public function see() {
  20. echo '我能看';
  21. }
  22. }
  23. class man extends person {
  24. use myTrait;
  25. public function work() {
  26. echo '我是超人';
  27. }
  28. public function see() {
  29. echo '超人就是我';
  30. }
  31. }
  32. $superMan = new man();
  33. $superMan->work();
  34. $superMan->run();
  35. $superMan->fly();
  36. $superMan->see();

演示截图

学习总结

有点难,只有搞点简单的,本来想像老师两种方法连接数据那样两种方法实现一个接口的,但是脑壳不怎么好使

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:再往后学就好了
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post