博客列表 >PHP 接口的使用场景,trait的使用

PHP 接口的使用场景,trait的使用

王娇
王娇原创
2020年04月30日 18:14:501308浏览

学习总结

  • 接口间接实现了多继承
  • 接口实现了多态
  • trait实现了类中代码的复用

    1.接口可能继承多个接口,类可以继承多个接口

  1. <?php
  2. //类只能单继承,接口允许多继承
  3. interface iSchool
  4. {
  5. const SCHOOL = '清华大学';
  6. public function printSchool();
  7. }
  8. interface iDepartment
  9. {
  10. const DEPARTMENT = '自动化系';
  11. public function printDepartment();
  12. }
  13. interface iClass
  14. {
  15. const CLASSES = '05-01';
  16. public function printClasses();
  17. }
  18. //类可以继承多个接口
  19. class StuInfo implements iSchool,iDepartment,iClass
  20. {
  21. protected $name = 'angle';
  22. protected $age = 32;
  23. protected $sex = '女';
  24. public function printSchool()
  25. {
  26. echo '校名:',iSchool::SCHOOL,'<br>';
  27. }
  28. public function printDepartment()
  29. {
  30. echo '系中:',iDepartment::DEPARTMENT,'<br>';
  31. }
  32. public function printClasses()
  33. {
  34. echo '班名:',iClass::CLASSES,'<br>';
  35. }
  36. public function editStuInfo($name,$age,$sex)
  37. {
  38. $this ->name = $name;
  39. $this ->age = $age;
  40. $this ->sex = $sex;
  41. }
  42. public function printStuInfo()
  43. {
  44. echo '姓名:',$this ->name,'<br>';
  45. echo '年龄:',$this ->age,'<br>';
  46. echo '性别:',$this ->sex,'<br>';
  47. $this ->printClasses();
  48. $this ->printDepartment();
  49. $this ->printSchool();
  50. echo '<hr>';
  51. }
  52. }
  53. $stu1 = new StuInfo();
  54. $stu1 -> printStuInfo();
  55. $stu2 = new StuInfo();
  56. $stu2 -> editStuInfo('hugn',31,'男');
  57. $stu2 -> printStuInfo();
  58. ?>
  • 代码运行结果

2.可以用抽象类部分实现接口中的功能,在工作类中再实现接口中的剩余功能

  1. <?php
  2. //可以使用抽象类部分实现接口
  3. interface iPrintInfo
  4. {
  5. public function editSDCInfo(string $school = '清华大学',string $department = '自动化系',string $classes = '05-01');
  6. public function printSDCInfo();
  7. }
  8. //抽象类实现了,学校名称,系名,班名的更改,并没有实现打印功能
  9. abstract class aStuInfo implements iPrintInfo
  10. {
  11. protected $school = '清华大学';
  12. protected $department = '自动化系';
  13. protected $classes = '05-01';
  14. public function editSDCInfo(string $school = '清华大学', string $department = '自动化系', string $classes = '05-01')
  15. {
  16. $this -> school = $school;
  17. $this -> department = $department;
  18. $this -> classes = $classes;
  19. }
  20. }
  21. //在工作类中实现学校名称,系名,班名输出
  22. class StuInfo extends aStuInfo
  23. {
  24. protected $name = 'angle';
  25. protected $age = 32;
  26. protected $sex = '女';
  27. public function editStuInfo($name,$age,$sex)
  28. {
  29. $this ->name = $name;
  30. $this ->age = $age;
  31. $this ->sex = $sex;
  32. }
  33. public function printStuInfo()
  34. {
  35. echo '姓名:',$this ->name,'<br>';
  36. echo '年龄:',$this ->age,'<br>';
  37. echo '性别:',$this ->sex,'<br>';
  38. $this ->printSDCInfo();
  39. }
  40. //接口中的方法没有在抽象类中实现,在工作类中实现
  41. public function printSDCInfo()
  42. {
  43. echo '班名:',$this ->classes,'<br>';
  44. echo '系名:',$this ->department,'<br>';
  45. echo '校名:',$this ->school,'<br>';
  46. echo '<hr>';
  47. }
  48. }
  49. $stu1 = new StuInfo();
  50. $stu1 ->printStuInfo();
  51. $stu2 = new StuInfo();
  52. $stu2 ->editStuInfo('hugn',31,'男');
  53. $stu2 -> editSDCInfo('人民大学','工商管理','08-01');
  54. $stu2 ->printStuInfo();
  55. ?>
  • 代码运行结果

3.接口中的构造函数被多个类继承,工作类中可以通过接口访问到每个类中的数据

  1. <?php
  2. interface iSchool
  3. {
  4. public function __construct();
  5. }
  6. class Qhdx implements iSchool
  7. {
  8. public $school = null;
  9. public $department = null;
  10. public $classes = null;
  11. public function __construct()
  12. {
  13. $this -> school = '清华大学';
  14. $this -> department = '自动化系';
  15. $this -> classes = '05-01';
  16. }
  17. }
  18. class Rmdx implements iSchool
  19. {
  20. public $school = null;
  21. public $department = null;
  22. public $classes = null;
  23. public function __construct()
  24. {
  25. $this -> school ='人民大学';
  26. $this -> department = '工商管理';
  27. $this -> classes = '08-02';
  28. }
  29. }
  30. class StuInfo
  31. {
  32. protected $name = 'angle';
  33. protected $age = 32;
  34. protected $sex = '女';
  35. public function editStuInfo($name,$age,$sex)
  36. {
  37. $this ->name = $name;
  38. $this ->age = $age;
  39. $this ->sex = $sex;
  40. }
  41. public function printStuInfo()
  42. {
  43. echo '姓名:',$this ->name,'<br>';
  44. echo '年龄:',$this ->age,'<br>';
  45. echo '性别:',$this ->sex,'<br>';
  46. }
  47. //学校,系,班信息为一个对象,iSchool是一个接口,被两个类继承
  48. public function printSDCInfo(iSchool $sInfo)
  49. {
  50. echo '班名:',$sInfo ->classes,'<br>';
  51. echo '系名:',$sInfo ->department,'<br>';
  52. echo '校名:',$sInfo ->school,'<br>';
  53. echo '<hr>';
  54. }
  55. }
  56. //学校信息通过不同的类实例化而成
  57. $sInfo = new Qhdx;
  58. $stu1 = new StuInfo;
  59. $stu1 ->printStuInfo();
  60. //打印学校信息时,传入的是一个对象
  61. $stu1 ->printSDCInfo($sInfo);
  62. $sInfo = new Rmdx;
  63. $stu2 = new StuInfo;
  64. $stu2 ->editStuInfo('hugn',31,'男');
  65. $stu2 ->printStuInfo();
  66. $stu2 ->printSDCInfo($sInfo);
  67. ?>
  • 代码运行结果

4.trait的使用,trait实现类中重复代码的复用

  1. <?php
  2. trait tPrintSDC
  3. {
  4. public function printSDCInfo()
  5. {
  6. echo '班名:',$this ->classes,'<br>';
  7. echo '系名:',$this ->department,'<br>';
  8. echo '校名:',$this ->school,'<br>';
  9. echo '<hr>';
  10. }
  11. public function editSDCInfo(string $school = '清华大学', string $department = '自动化系', string $classes = '05-01')
  12. {
  13. $this -> school = $school;
  14. $this -> department = $department;
  15. $this -> classes = $classes;
  16. }
  17. }
  18. class StuInfo
  19. {
  20. use tPrintSDC;
  21. protected $school = '清华大学';
  22. protected $department = '自动化系';
  23. protected $classes = '05-01';
  24. protected $name = 'angle';
  25. protected $age = 32;
  26. protected $sex = '女';
  27. public function editStuInfo($name,$age,$sex)
  28. {
  29. $this ->name = $name;
  30. $this ->age = $age;
  31. $this ->sex = $sex;
  32. }
  33. public function printStuInfo()
  34. {
  35. echo '姓名:',$this ->name,'<br>';
  36. echo '年龄:',$this ->age,'<br>';
  37. echo '性别:',$this ->sex,'<br>';
  38. $this ->printSDCInfo();
  39. }
  40. }
  41. $stu1 = new StuInfo;
  42. $stu1 ->printStuInfo();
  43. $stu2 = new StuInfo();
  44. $stu2 ->editStuInfo('hugn',31,'男');
  45. $stu2 -> editSDCInfo('人民大学','工商管理','08-01');
  46. $stu2 ->printStuInfo();
  47. ?>
  • 代码运行结果
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议