Blogger Information
Blog 64
fans 6
comment 2
visits 83018
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的继承,抽象类和接口的使用
王娇
Original
808 people have browsed it

学习总结

  • 类的继承:子类可以继承所有父类中的属性和方法,但是父类中的private属性在子类中是不能被访问的
  • 类的扩展:子类中除了使用父类的属性和方法,还可以添加自己的属性和方法
  • 类的重写:父类中的属性和方法可以在子类中被重新定义
  • 抽象类是不可以被实例化的类,但是可以被继承,实现了设计和实现部分分离
  • 接口中可以定义抽象方法(没有方法体的方法),普通类继承后必须重写并且实现该方法,实现了设计与实现完全分离

1.类的继承,类属性和方法的继承,扩展和重写

  1. <?php
  2. class StuInfo
  3. {
  4. protected $name = 'angle';
  5. protected $age = 32;
  6. protected $sex = '女';
  7. protected static $school = '人民大学';
  8. public function printInfo()
  9. {
  10. echo '姓名:',$this ->name,'<br>';
  11. echo '年龄:',$this ->age,'<br>';
  12. echo '性别:',$this ->sex,'<br>';
  13. }
  14. public static function printSchool()
  15. {
  16. echo '学校名称:',self::$school,'<br>','<hr>';
  17. }
  18. }
  19. //类的继承:子类继承父类的所有属性和方法
  20. class ClassInfo extends StuInfo
  21. {
  22. //子类可以对父类的属性进行扩展
  23. protected $class = '05-01';
  24. //子类可以对你类的方法进行重写
  25. public function printInfo()
  26. {
  27. parent::printInfo();
  28. echo '所在班级:',$this->class,'<br>';
  29. parent::printSchool();
  30. }
  31. //子类也可以对父类的方法进行扩展
  32. public function editInfo($name,$age,$sex,$class)
  33. {
  34. $this ->name = $name;
  35. $this ->age = $age;
  36. $this ->sex = $sex;
  37. $this ->class = $class;
  38. }
  39. }
  40. $stu1 = new ClassInfo();
  41. $stu1 ->printInfo();
  42. $stu2 = new ClassInfo();
  43. $stu2 ->editInfo('hugn',31,'男','05-02');
  44. $stu2 ->printInfo();
  45. ?>
  • 代码运行效果图

2.抽象类的使用

  1. <?php
  2. //抽象类不能被实例化,只能被继承,抽象类的作用,完成部分设计与实现分离
  3. abstract class StuInfo
  4. {
  5. protected $name = 'angle';
  6. protected $age = 32;
  7. protected $sex = '女';
  8. protected static $school = '人民大学';
  9. public function printInfo()
  10. {
  11. echo '姓名:',$this ->name,'<br>';
  12. echo '年龄:',$this ->age,'<br>';
  13. echo '性别:',$this ->sex,'<br>';
  14. }
  15. public static function printSchool()
  16. {
  17. echo '学校名称:',self::$school,'<br>';
  18. }
  19. }
  20. //抽象类不能被实例,只能被继承
  21. abstract class departInfo extends StuInfo
  22. {
  23. protected $department = '计算机系';
  24. public function printInfo()
  25. {
  26. parent::printInfo();
  27. echo '所在系:',$this -> department,'<br>';
  28. }
  29. }
  30. //普能类可以被实例化,可以继承抽象类
  31. class ClassInfo extends departInfo
  32. {
  33. protected $class = '05-01';
  34. public function printInfo()
  35. {
  36. parent::printInfo();
  37. echo '所在班级:',$this ->class,'<br>';
  38. parent::printSchool();
  39. echo '<hr>';
  40. }
  41. public function editInfo($name,$age,$sex,$department = '计算机系',$class = '05-01')
  42. {
  43. $this ->name = $name;
  44. $this ->age = $age;
  45. $this ->sex = $sex;
  46. $this ->department = $department;
  47. $this ->class = $class;
  48. }
  49. public static function editSchool($school)
  50. {
  51. self::$school = $school;
  52. }
  53. }
  54. $stu1 = new ClassInfo();
  55. $stu1 ->printInfo();
  56. $stu2 = new ClassInfo();
  57. $stu2 ->editInfo('hugn',31,'男');
  58. $stu2 ->printInfo();
  59. $stu3 = new ClassInfo();
  60. $stu3 ->editInfo('lucy',35,'女','工商管理','06-02');
  61. $stu3 ->editSchool('清华大学');
  62. $stu3 ->printInfo();
  63. ?>
  • 代码运行效果图

3.接口的使用

  1. <?php
  2. //接口的定义与类的定义相类似
  3. interface iStuInfo
  4. {
  5. const CITY = '北京';
  6. public function printInfo();
  7. public function editInfo($name,$age,$sex,$department = '计算机系',$class = '05-01');
  8. public function printCity();
  9. }
  10. class StuInfo implements iStuInfo
  11. {
  12. protected $name = 'angle';
  13. protected $age = 32;
  14. protected $sex = '女';
  15. protected $class = '05-01';
  16. protected $department = '计算机系';
  17. protected static $school = '人民大学';
  18. public function printInfo()
  19. {
  20. echo '姓名:',$this ->name,'<br>';
  21. echo '年龄:',$this ->age,'<br>';
  22. echo '性别:',$this ->sex,'<br>';
  23. echo '班级:',$this ->class,'<br>';
  24. echo '所在系:',$this ->department,'<br>';
  25. echo '学校名称:',self::$school,'<br>';
  26. }
  27. public function editInfo($name,$age,$sex,$department = '计算机系',$class = '05-01')
  28. {
  29. $this ->name = $name;
  30. $this ->age = $age;
  31. $this ->sex = $sex;
  32. $this ->department = $department;
  33. $this ->class = $class;
  34. }
  35. public function printCity()
  36. {
  37. echo '所在城市:',iStuInfo::CITY,'<br>';
  38. echo '<hr>';
  39. }
  40. }
  41. $stu1 = new StuInfo();
  42. $stu1 ->printInfo();
  43. $stu1 ->printCity();
  44. $stu2 = new StuInfo();
  45. $stu2 ->editInfo('hugn',31,'男');
  46. $stu2 ->printInfo();
  47. $stu2 ->printCity();
  48. ?>
  • 代码运行效果图
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