Blogger Information
Blog 55
fans 3
comment 0
visits 54034
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
抽象类与接口实战
王佳祥
Original
875 people have browsed it

抽象类与接口实战

一、抽象类

什么是抽象类

  • 通过abstract关键字定义的类为抽象类,抽象类:部分分离了“设计与实现”。
  1. <?php
  2. //父类通过abstract关键字设置为抽象类后不能被实例化
  3. //抽象类:分离了“设计与实现”
  4. abstract class user
  5. {
  6. protected $name = '张起灵';
  7. public function write()
  8. {
  9. return '姓名:' .$this -> name;
  10. }
  11. //抽象方法:,没有实现,只有声明,子类中必须实现它
  12. abstract protected function write2();
  13. }
  14. //子类,实现类,工作类,真正干活的是子类
  15. class Work extends user
  16. {
  17. protected $profession = '守护者';
  18. public function write2()
  19. {
  20. return parent::write().'职业:'.$this->profession;
  21. }
  22. }
  23. //必须通过子类来调用抽象方法
  24. $user = new Work();
  25. //echo $user->write();
  26. echo $user->write2();


二、接口

1.单接口

  1. <?php
  2. //接口:完全分离了“设计与实现”
  3. //1.接口使用场景:单接口
  4. interface iUser
  5. {
  6. //在接口中所有成员都是抽象的
  7. //接口成员的访问控制必须是public
  8. public function write();
  9. //不允许有属性,只允许定义接口常量
  10. const NAME = 'ADMIN';
  11. //接口还可以有构造方法(后面讲)
  12. }
  13. //接口的实现类
  14. class user implements iUser
  15. {
  16. protected $profession = '警察';
  17. //接口中的抽象方法必须在实现类中实现
  18. public function write()
  19. {
  20. return '职业:'.$this->profession;
  21. }
  22. }
  23. //调用接口
  24. $user = new user();
  25. echo $user->write();


2.接口之间的继承

  1. <?php
  2. //接口之间的继承
  3. //php中禁止多重继承:单继承
  4. //接口1
  5. interface iUser1
  6. {
  7. const AGE = 2000;
  8. }
  9. //接口2
  10. interface iUser2 extends iUser1
  11. {
  12. const USER_NAME = '观音菩萨';
  13. }
  14. //接口3
  15. interface iUser3 extends iUser2
  16. {
  17. public function write();
  18. }
  19. //实现类
  20. class User implements iUser3
  21. {
  22. //必须实现接口中的抽象方法
  23. public function write()
  24. {
  25. return '姓名:'.iUser2::USER_NAME.' 年龄:'.iUser1::AGE;
  26. }
  27. }
  28. $user = new User;
  29. echo $user->write();


3. 用抽象类来实现接口

  1. <?php
  2. //接口应用场景3:用抽象类来实现接口
  3. //定义一个数据库的CURD
  4. interface iDbBase
  5. {
  6. //新增
  7. public static function insert($db,$data);
  8. //查询
  9. public static function select($db,$options=[]);
  10. //更新
  11. public static function update($db,$options);
  12. //删除
  13. public static function delete($dbm,$where);
  14. }
  15. //实现类:使用抽象类来充当
  16. abstract class aDb implements iDbBase
  17. {
  18. //使用单例模式连接:创建类的唯一实例,唯一对象
  19. protected static $db =null;
  20. //抽象类充当接口实现类时,不用必须实现接口中的抽象方法
  21. //当实现类中的方法中有一些公共操作时,可以将这些操作放在中间的抽象类中实现它
  22. public static function connect($dsn,$username,$password)
  23. {
  24. if(is_null(self::$db)){
  25. //如果当前连接对象时null,表示未连接数据库
  26. self::$db = new PDO($dsn,$username,$password);
  27. }
  28. return self::$db;
  29. }
  30. }
  31. //类的正真实现类
  32. class DB extends aDb
  33. {
  34. //新增
  35. public static function insert($db,$data)
  36. {
  37. }
  38. //查询
  39. public static function select($db,$options=[])
  40. {
  41. return $db->query('SELECT * FROM `user` LIMIT 3')->fetchAll(PDO::FETCH_ASSOC);
  42. }
  43. //更新
  44. public static function update($db,$data)
  45. {
  46. }
  47. //删除
  48. public static function delete($db,$data)
  49. {
  50. }
  51. }
  52. //连接数据库
  53. //连接参数
  54. $config = [
  55. //类型
  56. 'type' => $type ?? 'mysql',
  57. //默认数据库主机名(IP)
  58. 'host' => $host ?? 'localhost',
  59. //默认数据库名
  60. 'dbname' => $type ?? 'tp5',
  61. //默认字符编码集
  62. 'charset' => $type ?? 'utf8',
  63. //默认端口号
  64. 'port' => $username ?? '3306',
  65. //默认用户名
  66. 'username' => $username ?? 'root',
  67. //默认用户的密码
  68. 'password' => $password ?? 'wang1111'
  69. ];
  70. $dsn = sprintf('%s:host=%s;dbname=%s;',$config['type'],$config['host'],$config['dbname']);
  71. $username = $config['username'];
  72. $password = $config['password'];
  73. $db = DB::connect($dsn,$username,$password);
  74. //调用实现类DB中的select()进行查询
  75. foreach (DB::select($db) as $user){
  76. print_r($user);
  77. }


三、Trait

  1. <?php
  2. //Trait:特性
  3. //trait:临时类,类中类,可以嵌入到宿主类
  4. //trait实现了一个类的横向扩展,继承实现了类的垂直功能扩展
  5. //可以使用类的语句,但不是类
  6. //不能被直接实例化
  7. trait tDemo
  8. {
  9. //1.常规成员
  10. protected $name = '胡歌';
  11. public function getName()
  12. {
  13. return $this->name;
  14. }
  15. //2.静态成员
  16. protected static $sex = '男';
  17. public static function getSex()
  18. {
  19. return self::$sex;
  20. }
  21. //3.抽象成员
  22. public static $age;
  23. abstract static function getAge();
  24. }
  25. //在一个宿主类/工作类/实现类中使用
  26. class User
  27. {
  28. //类中使用use关键字引用trait成员
  29. use tDemo;
  30. //在宿主类中必须将trait中的抽象方法实现了
  31. static function getAge()
  32. {
  33. return self::$age;
  34. }
  35. }
  36. $user = new User;
  37. User::$age = 20;
  38. echo $user->getName().':'.$user->getSex().'|'.$user->getAge();


四、学习总结

1.抽象类

  • 通过abstract关键字定义的类为抽象类,抽象类:部分分离了“设计与实现”

  • 抽象类不能被实例化

  • 通过子类来调用抽象方法

  • 任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的

  • 继承一个抽象类的时候,子类必须实现父类中的所有抽象方法

2.接口

  • 通过interface关键字来定义接口

  • 对接口的使用是通过关键字implements

  • 接口不能定义成员变量(包括类静态变量),能定义常量

  • 子类必须实现接口定义的所有方法

  • 接口只能定义不能实现该方法

  • 接口中的方法和实现它的类默认都是public类型的

3.Static类/方法

  • 可以不实例化类而直接访问

  • 静态属性不可以由对象通过->操作符来访问,用::方式调用

4.Trait

  • Trait字面意思是”特征,特点”,我们可以理解为使用trait关键字,可以给php的类添加新的特性

  • trait关键字来定义trait类

  • trait实现了一个类的横向扩展,继承实现了类的垂直功能扩展

  • use关键字来引用trait成员

  • 可以使用类的语句,但不是类

  • 不能被实例化

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:为什么明明用类也可以, 为什么要发明出抽象类,接口, trait这些东西, 想过吗?
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!