Blogger Information
Blog 16
fans 7
comment 1
visits 11368
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月29日- 抽象类和接口
Eric
Original
589 people have browsed it

一、自动加载技术

  1. class Loader{
  2. public static function load($className)
  3. {
  4. $path = $className.'.php';
  5. if (file_exists($path)){
  6. require $path;
  7. }else{
  8. echo $path . '不存在,请检查~~';
  9. }
  10. }
  11. }

工作类 index.php

  1. require 'Loader.php';
  2. spl_autoload_register(array('Loader','load'));
  3. use admin\Car;
  4. class Demo{
  5. function show(Car $car){
  6. return $car->drive();
  7. }
  8. }
  9. $car = new Car();
  10. $demo = new Demo();
  11. echo $demo->show($car);

二、抽象类

abstract: 定义抽象方法/抽象类
类中只要有一个抽象方法, 该类就应该声明为抽象类
抽象类只能被继承,不能实例化,并且抽象方法必须在子类实现

  1. abstract class AbstractClass
  2. {
  3. public $name;
  4. public function __construct($name)
  5. {
  6. $this->name = $name;
  7. }
  8. abstract protected function hello();
  9. abstract protected function say();
  10. public function test(){
  11. return '这里是普通方法';
  12. }
  13. }
  14. class ChildClass extends AbstractClass {
  15. public function hello()
  16. {
  17. return 'hello,'.$this->name;
  18. }
  19. public function say()
  20. {
  21. return 'say,'.$this->name;
  22. }
  23. }
  24. $obj = new ChildClass('孙悟空');
  25. echo $obj->test(); //这里是普通方法
  26. echo $obj->hello(); //hello,孙悟空
  27. echo $obj->say(); //say,孙悟空

三、接口

interface: 指定某个类必须实现的方法,但不需要定义方法的具体实现过程
接口中仅允许出现: 方法与类常量
接口的方法可见性必须是: public
接口的方法体必须是空的
接口是类的代码模板, 可以像类一样有父子继承关系,例如父接口, 子接口

  1. //接口一
  2. interface Idemo1
  3. {
  4. const SITE_NAME = 'www.php.cn';
  5. public function show();
  6. public function hide();
  7. }
  8. //接口二
  9. interface Idemo2
  10. {
  11. public function increase();
  12. public function decrease();
  13. }
  14. //工作类
  15. class Demo implements Idemo1,Idemo2{
  16. public function show()
  17. {
  18. return '显示方法'.Idemo1::SITE_NAME;
  19. }
  20. public function hide()
  21. {
  22. return '影藏方法'.Idemo1::SITE_NAME;
  23. }
  24. public function increase()
  25. {
  26. return '增加方法'.Idemo1::SITE_NAME;
  27. }
  28. public function decrease()
  29. {
  30. return '减少方法'.Idemo1::SITE_NAME;
  31. }
  32. }
  33. $obj = new Demo();
  34. $obj->show();
  35. $obj->hide();
  36. $obj->increase();
  37. $obj->decrease();

手写:


课程总结:

1、文件自动加载使用函数spl_autoload_register
2、抽象类的抽象方法在子类必须实现,抽象类可以有普通方法
3、接口类似抽象方法,接口的方法也必须在实现类实现,接口方法也没有方法体,可以有常量。

THE END !

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!