Blogger Information
Blog 17
fans 1
comment 0
visits 20075
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Trait和抽象方法的综合示例
大A
Original
963 people have browsed it

示例介绍

  • 用表格的方式展示学生成绩单
  • 查询指定学生的单科成绩
  • 删除指定学生的所有成绩

示例代码

  1. <?php
  2. //建立一个抽象类,类内包含成绩表数组,打印成绩表,和一个查询用的抽象方法
  3. abstract class Score
  4. {
  5. protected $data = [
  6. ['姓名' => '张三', '语文' => '78', '数学' => '85'],
  7. ['姓名' => '李四', '语文' => '45', '数学' => '55'],
  8. ['姓名' => '王五', '语文' => '85', '数学' => '63'],
  9. ['姓名' => '任六', '语文' => '68', '数学' => '57'],
  10. ['姓名' => '王伟', '语文' => '88', '数学' => '89'],
  11. ['姓名' => '李刚', '语文' => '82', '数学' => '90'],
  12. ];
  13. private function format($a, $b)
  14. {
  15. return $a .= ('<tr><th>' .
  16. $b['姓名'] . '</th><th>' .
  17. $b['语文'] . '</th><th>' .
  18. $b['数学'] . '</th></tr>');
  19. }
  20. public function showData()
  21. {
  22. echo array_reduce(
  23. $this->data,
  24. [$this, 'format'],
  25. '<table border="1" cellspacing="0"><tr><th>姓名</th><th>语文</th><th>数学</th></tr>'
  26. ) . '</table><hr>';
  27. }
  28. public abstract function search($name, $subject);
  29. }
  30. //创建一个包含所有查询方法的trait
  31. trait scoreSearch
  32. {
  33. //查询单人的所有成绩,查询成功返回原数组内索引和对应的键值
  34. protected function search_array($data, $name)
  35. {
  36. foreach ($data as $key => $value) {
  37. if (!array_search($name, $value) === false) :
  38. $result = [$key, $value];
  39. break;
  40. endif;
  41. $result = '没有此人.<hr>';
  42. }
  43. return $result;
  44. }
  45. //查询出单科的成绩
  46. protected function single_score($array, $subject, &$score)
  47. {
  48. if (!array_key_exists($subject, $array[1])) return false;
  49. $score = $array[1][$subject];
  50. if ($score === false) :
  51. return false;
  52. endif;
  53. return true;
  54. }
  55. }
  56. //查询学生的单科成绩
  57. trait operation1
  58. {
  59. use scoreSearch;
  60. protected function operation($data, $name, $subject = null)
  61. {
  62. $array = $this->search_array($data, $name);
  63. if (is_string($array)) return $array;
  64. if ($this->single_score($array, $subject, $score)) :
  65. return $name . '的' . $subject . '成绩是' . $score . '分<hr>';
  66. else :
  67. return '查询的科目不存在<hr>';
  68. endif;
  69. }
  70. }
  71. //删除表格内单个学生的所有成绩
  72. trait operation2
  73. {
  74. use scoreSearch;
  75. //删除单个学生的所有成绩
  76. private function operation(&$array, $name)
  77. {
  78. $result = $this->search_array($array, $name);
  79. if (is_string($result)) return '删除失败,没有此人.<hr>';
  80. $size1 = count($array);
  81. unset($array[$result[0]]);
  82. $size2 = count($array);
  83. return $size1 > $size2 ? $name.'被成功删除!<hr>' : '删除失败<hr>';
  84. }
  85. }
  86. //创建一个工作类继承Score类,用于查询和删除数据
  87. class Export extends Score
  88. {
  89. //两个trait的方法名冲突,将operation2内的方法设置别名为del_value并将权限设置为public
  90. use operation1, operation2 {
  91. operation1::operation insteadof operation2;
  92. operation2::operation as public del_value;
  93. }
  94. //查询学生的单科成绩
  95. public function search($name, $subject)
  96. {
  97. return $this->operation($this->data, $name, $subject);
  98. }
  99. //删除指定学生所有成绩
  100. public function del_array($name)
  101. {
  102. echo $this->del_value($this->data, $name);
  103. }
  104. }
  105. //将工作对象实例化
  106. $ex = new Export;
  107. //显示成绩表
  108. echo $ex->showData();
  109. //查询学生成绩
  110. echo $ex->search('李刚', '数学');
  111. //查询学生名不存在时
  112. echo $ex->search('jim', '数学');
  113. //查询科目不存在时
  114. echo $ex->search('李四', '地理');
  115. //删除学生成绩
  116. $ex->del_array('王五');
  117. $ex->del_array('王伟');
  118. //删除学生不存在时
  119. $ex->del_array('李磊');
  120. //显示成绩表
  121. echo $ex->showData();

运行结果

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