接口和抽象类的实现-区别与联系

作业内容:自己定义一个接口和抽象类,并实现它,对比接口与抽象类的区别与联系,并实例演示

  • 接口: 只允许两类成员:类常量和公共抽象方法,支持构造方法;
  • 抽象类: 可以允许受保护的成员;

  • 接口: 接口中的方法,只有一个声明,没有函数体;

  • 抽象类: 抽象类中的方法,有声明抽象方法的,没有函数体,非抽象方法可以有函数体,可以有或没有实现过程;

  • 接口: 接口中的方法,类通过 implements 实施接口,接口中的方法必须全部实现;

  • 抽象类: 抽象类中的方法,通过子类 extends 继承抽象类,抽象类中有抽象方法的,子类必须全部实现,非抽象方法的,子类可以不必实现;

  • 接口和抽象类: 都可以继承;但都不能实例化;

  1. <?php
  2. // 接口和抽象类的实现
  3. // 定义接口
  4. // 只允许使用常量和公共方法,方法只有一个声明,没有函数体;
  5. interface iArrs
  6. {
  7. // 常量
  8. const IARRS = 'interface';
  9. // 构造方法
  10. public function __construct($datas);
  11. // 增
  12. public function add($data);
  13. // 删
  14. public function del($ids);
  15. // 改
  16. public function change($id, $num = null, $title = null);
  17. // 查
  18. public function find($id);
  19. }
  20. // 抽象类
  21. // 有声明抽象方法的,没有函数体,非抽象方法可以有函数体,可以有或没有实现过程;
  22. abstract class aArrs
  23. {
  24. // 常量和公共变量
  25. const AARRS = 'abstract';
  26. public $aArrs = 'abstract aArrs';
  27. // 搜
  28. // 声明为抽象方法,子类中必须实现它
  29. abstract protected function search($words, $ignore_case = false);
  30. // 序
  31. // 这个公开方法,有函数体,没有实现过程
  32. public function sort($field = 'id', $order = SORT_ASC)
  33. {
  34. }
  35. }
  36. // 工作类
  37. // 实现接口和继承抽象类的方法的全部实现
  38. // 继承抽象,使用接口
  39. class Arrs extends aArrs implements iArrs
  40. {
  41. private $datas;
  42. // 构造方法
  43. public function __construct($datas)
  44. {
  45. $this->datas = $datas;
  46. }
  47. // 增
  48. public function add($data)
  49. {
  50. array_push($this->datas, $data);
  51. }
  52. // 删
  53. public function del($ids)
  54. {
  55. $this->datas = array_diff_assoc($this->datas, array_filter($this->datas, function ($data) use ($ids) {
  56. return in_array($data['id'], (array)$ids);
  57. }));
  58. }
  59. // 改
  60. public function change($id, $num = null, $title = null)
  61. {
  62. $this->datas = array_map(function ($data) use ($id, $num, $title) {
  63. if ($data['id'] === $id) {
  64. if (!is_null($num)) $data['num'] = $num;
  65. if (!is_null($title)) $data['title'] = $title;
  66. }
  67. return $data;
  68. }, $this->datas);
  69. }
  70. // 查
  71. public function find($id)
  72. {
  73. return array_shift(array_filter($this->datas, function ($data) use ($id) {
  74. return $data['id'] === $id;
  75. }));
  76. }
  77. // 搜
  78. public function search($words, $ignore_case = false)
  79. {
  80. return array_filter($this->datas, function ($data) use ($words, $ignore_case) {
  81. return false !== call_user_func($ignore_case ? 'stripos' : 'strpos', $data['title'], $words);
  82. });
  83. }
  84. // 序
  85. public function sort($field = 'id', $order = SORT_ASC)
  86. {
  87. array_multisort($this->datas, $order, array_column($this->datas, $field));
  88. }
  89. // 返回数据
  90. public function datas()
  91. {
  92. return $this->datas;
  93. }
  94. }
  95. // 准备数据
  96. $datas = [
  97. ['id' => 3, 'num' => 11, 'title' => 'css'],
  98. ['id' => 2, 'num' => 14, 'title' => 'html'],
  99. ['id' => 4, 'num' => 12, 'title' => 'javascript'],
  100. ['id' => 1, 'num' => 13, 'title' => 'hello world!'],
  101. ];
  102. $data = ['id' => 5, 'num' => 15, 'title' => 'hello php!'];
  103. // 实例化对象
  104. $arrs = new Arrs($datas);
  105. // 接口常量访问
  106. // interface
  107. echo iArrs::IARRS, '<br>';
  108. // 抽象类常量和公开成员方法
  109. // abstract | abstract aArrs
  110. echo AArrs::AARRS, ' | ', $arrs->aArrs, '<br>';
  111. // 工作对象
  112. // 增
  113. $arrs->add($data);
  114. /* Array (
  115. [0] => Array ( [id] => 3 [num] => 11 [title] => css )
  116. [1] => Array ( [id] => 2 [num] => 14 [title] => html )
  117. [2] => Array ( [id] => 4 [num] => 12 [title] => javascript )
  118. [3] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
  119. [4] => Array ( [id] => 5 [num] => 15 [title] => hello php! )
  120. )
  121. */
  122. echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';
  123. // 删
  124. // 删除 id = 3 和 id = 5 的记录
  125. $arrs->del([3, 5]);
  126. /*
  127. Array (
  128. [1] => Array ( [id] => 2 [num] => 14 [title] => html )
  129. [2] => Array ( [id] => 4 [num] => 12 [title] => javascript )
  130. [3] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
  131. )
  132. */
  133. echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';
  134. // 改
  135. // 改变 id = 2 的 num = 10 和 title = 'hello php!'
  136. $arrs->change(2, 10, 'hello php!');
  137. /* Array (
  138. [1] => Array ( [id] => 2 [num] => 10 [title] => hello php! )
  139. [2] => Array ( [id] => 4 [num] => 12 [title] => javascript )
  140. [3] => Array ( [id] => 1 [num] => 13 [title] => hello world! ) )
  141. */
  142. echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';
  143. // 查
  144. // 查找 id = 4 的记录
  145. // Array ( [id] => 4 [num] => 12 [title] => javascript )
  146. echo '<pre>' . print_r($arrs->find(4), true), '</pre><br>';
  147. // 搜
  148. // 搜索 title 包含关键词 hello 的记录
  149. /* Array (
  150. [1] => Array ( [id] => 2 [num] => 10 [title] => hello php! )
  151. [3] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
  152. )
  153. */
  154. echo '<pre>' . print_r($arrs->search('hello', true), true), '</pre><br>';
  155. // 序
  156. // 按 id 升序
  157. $arrs->sort();
  158. /* Array (
  159. [0] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
  160. [1] => Array ( [id] => 2 [num] => 10 [title] => hello php! )
  161. [2] => Array ( [id] => 4 [num] => 12 [title] => javascript )
  162. )
  163. */
  164. echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';
  165. // 按 id 降序
  166. $arrs->sort('id', SORT_DESC);
  167. /* Array (
  168. [0] => Array ( [id] => 4 [num] => 12 [title] => javascript )
  169. [1] => Array ( [id] => 2 [num] => 10 [title] => hello php! )
  170. [2] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
  171. )
  172. */
  173. echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';

接口和抽象类

更多相关文章

  1. PHP面向对象编程
  2. 类声明-实例化-成员-继承-trait特征类
  3. Java基础知识点1
  4. 全面演示: 函数的作用域与闭包,回调的使用场景以及函数的多值返
  5. [计算机视觉论文速递] 2018-03-14
  6. [计算机视觉论文速递] 2018-03-16
  7. [计算机视觉论文速递] 2018-03-20
  8. [计算机论文速递] 2018-04-13
  9. [计算机视觉论文速递] 2018-05-10

随机推荐

  1. [转]新浪微博AppKey大集合
  2. ViewPager实现一个页面多个Item的显示
  3. Android CTS Tips
  4. CMD命令创建、编译Android应用程序
  5. Android VideoPlayer
  6. Android 5.1状态栏背景色定制 在Launcher
  7. Android中javax annotation Nullable找不
  8. Android 开源优秀项目
  9. Android studio 运行出现Error running a
  10. android Standard Broadcast Actions 的