Blogger Information
Blog 28
fans 0
comment 0
visits 15761
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
08-11 作业 面对对象和自动加载 封装 继承 多态
︷肉園耔︷
Original
644 people have browsed it
  1. <?php
  2. //array_map() 可以使用用户自定义的函数对数组中的每个元素做回调处理 成功返回true, 失败返回false
  3. $fruits =array('a'=>'apple','b'=>'banana','o'=>'ornge','1'=>'lemon');
  4. $fav='orange';
  5. function test(&$item,$key,$prefix){
  6. $item="$prefix:$key=>$item";
  7. }
  8. array_walk($fruits,'test','fruit');
  9. echo '<pre>';
  10. print_r($fruits);
  11. //arraya_map(callback,arr1,arr2 。。。)
  12. $arr = array_map(function($v){
  13. if($v>25) return $v;
  14. },[25,12,521,58,556]);
  15. echo '<pre>';
  16. print_r($arr);
  17. //使用array_filter()使用回调函数过滤数组的元素 返回是过滤后的数组
  18. /*$arr=array_filter($arr);
  19. print_r($arr);*/
  20. //array_values()给数组的键重新排序,从0递增
  21. var_dump($arr =array_values(array_filter($arr)));
  22. ob_clean();
  23. $a=[25,12,521,58,556];
  24. $res=array_values( array_filter($a,function($b){
  25. return $b>25;
  26. }));
  27. echo '<pre>';
  28. print_r($res);
  29. <?php
  30. /*
  31. * 1.变量 实现数据的复用 函数是实现代码块的复用
  32. * 2.类是具有相同属性和方法的对象的集合
  33. * 3.对象是复合数据类型
  34. * 4.oop三大特性 封装 继承 多态
  35. * //封装:隐藏对象的属性或方法(实现细节),目的是为了控制在程序中属性的读或者修改的访问级别,使用者只需要通过外部接口 以特定的权限来使用类成员
  36. *
  37. * */
  38. class Player{
  39. //成员属性 前一定要有访问修饰符 public protected private
  40. public $name; //抽象属性 null
  41. public $height;
  42. public $team;
  43. //protected 只能被本类和子类访问
  44. protected $playNum=23;
  45. private $weight;
  46. public function jog(){
  47. return "$this->name is jogging,whose playNum is $this->playNum<br>";
  48. }
  49. public function shoot(){
  50. return "$this->name is shooting weighing $this->height"; }
  51. //__construct
  52. public function __construct($name,$height,$team,$playNum,$weight){
  53. $this->name=$name;
  54. $this->height=$height;
  55. $this->team=$team;
  56. $this->playNum=$playNum;
  57. $this->weight=$weight;
  58. }
  59. }
  60. //类的实例化 new $j对象引用
  61. /* $jordan=new Player;
  62. $james=new Player;
  63. 对象成员的访问 对象引用 -》属性名称/方法名称()
  64. $jordan->name ='Jordan';
  65. $james ->name ='James';
  66. echo $jordan->name.'<br>';
  67. echo $jordan->jog();
  68. echo $james->name.'<br>';
  69. echo $james->jog();*/
  70. $jordan = new Player('Jordan','203cm','bulk',23,'80KG');
  71. echo $jordan->name.'<br>';
  72. echo $jordan->shoot();
  73. <?php
  74. //客户端代码 调用封装的类
  75. //1.加载类文件
  76. //require '5.php';
  77. //类的自动加载
  78. spl_autoload_register(function($className){
  79. require $className.'.php';
  80. });
  81. //2.new类的实例化
  82. $j= new User('Jordan','203CM','Bulk',23,'80kg');
  83. echo $j->shoot();
Correcting teacher:PHPzPHPz

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