批改状态:合格
老师批语:
作业内容:自己定义一个接口和抽象类,并实现它,对比接口与抽象类的区别与联系,并实例演示
抽象类: 可以允许受保护的成员;
接口: 接口中的方法,只有一个声明,没有函数体;
抽象类: 抽象类中的方法,有声明抽象方法的,没有函数体,非抽象方法可以有函数体,可以有或没有实现过程;
接口: 接口中的方法,类通过 implements 实施接口,接口中的方法必须全部实现;
抽象类: 抽象类中的方法,通过子类 extends 继承抽象类,抽象类中有抽象方法的,子类必须全部实现,非抽象方法的,子类可以不必实现;
接口和抽象类: 都可以继承;但都不能实例化;
<?php// 接口和抽象类的实现// 定义接口// 只允许使用常量和公共方法,方法只有一个声明,没有函数体;interface iArrs{// 常量const IARRS = 'interface';// 构造方法public function __construct($datas);// 增public function add($data);// 删public function del($ids);// 改public function change($id, $num = null, $title = null);// 查public function find($id);}// 抽象类// 有声明抽象方法的,没有函数体,非抽象方法可以有函数体,可以有或没有实现过程;abstract class aArrs{// 常量和公共变量const AARRS = 'abstract';public $aArrs = 'abstract aArrs';// 搜// 声明为抽象方法,子类中必须实现它abstract protected function search($words, $ignore_case = false);// 序// 这个公开方法,有函数体,没有实现过程public function sort($field = 'id', $order = SORT_ASC){}}// 工作类// 实现接口和继承抽象类的方法的全部实现// 继承抽象,使用接口class Arrs extends aArrs implements iArrs{private $datas;// 构造方法public function __construct($datas){$this->datas = $datas;}// 增public function add($data){array_push($this->datas, $data);}// 删public function del($ids){$this->datas = array_diff_assoc($this->datas, array_filter($this->datas, function ($data) use ($ids) {return in_array($data['id'], (array)$ids);}));}// 改public function change($id, $num = null, $title = null){$this->datas = array_map(function ($data) use ($id, $num, $title) {if ($data['id'] === $id) {if (!is_null($num)) $data['num'] = $num;if (!is_null($title)) $data['title'] = $title;}return $data;}, $this->datas);}// 查public function find($id){return array_shift(array_filter($this->datas, function ($data) use ($id) {return $data['id'] === $id;}));}// 搜public function search($words, $ignore_case = false){return array_filter($this->datas, function ($data) use ($words, $ignore_case) {return false !== call_user_func($ignore_case ? 'stripos' : 'strpos', $data['title'], $words);});}// 序public function sort($field = 'id', $order = SORT_ASC){array_multisort($this->datas, $order, array_column($this->datas, $field));}// 返回数据public function datas(){return $this->datas;}}// 准备数据$datas = [['id' => 3, 'num' => 11, 'title' => 'css'],['id' => 2, 'num' => 14, 'title' => 'html'],['id' => 4, 'num' => 12, 'title' => 'javascript'],['id' => 1, 'num' => 13, 'title' => 'hello world!'],];$data = ['id' => 5, 'num' => 15, 'title' => 'hello php!'];// 实例化对象$arrs = new Arrs($datas);// 接口常量访问// interfaceecho iArrs::IARRS, '<br>';// 抽象类常量和公开成员方法// abstract | abstract aArrsecho AArrs::AARRS, ' | ', $arrs->aArrs, '<br>';// 工作对象// 增$arrs->add($data);/* Array ([0] => Array ( [id] => 3 [num] => 11 [title] => css )[1] => Array ( [id] => 2 [num] => 14 [title] => html )[2] => Array ( [id] => 4 [num] => 12 [title] => javascript )[3] => Array ( [id] => 1 [num] => 13 [title] => hello world! )[4] => Array ( [id] => 5 [num] => 15 [title] => hello php! ))*/echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';// 删// 删除 id = 3 和 id = 5 的记录$arrs->del([3, 5]);/*Array ([1] => Array ( [id] => 2 [num] => 14 [title] => html )[2] => Array ( [id] => 4 [num] => 12 [title] => javascript )[3] => Array ( [id] => 1 [num] => 13 [title] => hello world! ))*/echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';// 改// 改变 id = 2 的 num = 10 和 title = 'hello php!'$arrs->change(2, 10, 'hello php!');/* Array ([1] => Array ( [id] => 2 [num] => 10 [title] => hello php! )[2] => Array ( [id] => 4 [num] => 12 [title] => javascript )[3] => Array ( [id] => 1 [num] => 13 [title] => hello world! ) )*/echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';// 查// 查找 id = 4 的记录// Array ( [id] => 4 [num] => 12 [title] => javascript )echo '<pre>' . print_r($arrs->find(4), true), '</pre><br>';// 搜// 搜索 title 包含关键词 hello 的记录/* Array ([1] => Array ( [id] => 2 [num] => 10 [title] => hello php! )[3] => Array ( [id] => 1 [num] => 13 [title] => hello world! ))*/echo '<pre>' . print_r($arrs->search('hello', true), true), '</pre><br>';// 序// 按 id 升序$arrs->sort();/* Array ([0] => Array ( [id] => 1 [num] => 13 [title] => hello world! )[1] => Array ( [id] => 2 [num] => 10 [title] => hello php! )[2] => Array ( [id] => 4 [num] => 12 [title] => javascript ))*/echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';// 按 id 降序$arrs->sort('id', SORT_DESC);/* Array ([0] => Array ( [id] => 4 [num] => 12 [title] => javascript )[1] => Array ( [id] => 2 [num] => 10 [title] => hello php! )[2] => Array ( [id] => 1 [num] => 13 [title] => hello world! ))*/echo '<pre>' . print_r($arrs->datas(), true), '</pre><br>';

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号