Blogger Information
Blog 26
fans 0
comment 0
visits 18137
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的继承,抽象类以及接口的基本语法
雪~人胖胖
Original
946 people have browsed it

类的继承

  1. //父类
  2. class User
  3. { //public:公开成员 类的继承上下文环境中
  4. //protected:受保护的成员,类外部禁止访问,类的内部以及它的继承上下文中可以访问
  5. //private:私有成员,只能在当前类访问
  6. public $name = '张三';
  7. protected $age = 50;
  8. protected $salary = 9999;
  9. public static $nationality = '中国/CHINA';
  10. //类中方法可以访问类中所有成员
  11. public function write(){
  12. return "姓名:{$this->name},年龄:{$this->age},工资:{$this->salary}";
  13. }
  14. }
  15. //子类
  16. class Rob extends User{
  17. //1.继承:父类的public和protected自动成为子类的成员
  18. //2.重写:覆写与父类同名的成员(属性和方法)
  19. //3.扩展:子类添加自身的方法来增加/扩展父类的功能
  20. //1.重写父类成员
  21. //属性重写
  22. public $name = '李四';
  23. //方法重写 在子类中引用父类成员用parent:: 如果想禁止方法重写在作用域public前加上final 如果想禁止整个类被重写在class前加final
  24. public function write(){
  25. //return "姓名:{$this->name},年龄:{$this->age},工资:{$this->salary}.是一个摸金的~~~。";
  26. return parent::write().',是一个摸金的~~~。';
  27. }
  28. //2.扩展
  29. //属性扩展
  30. protected $profession = '摸金校尉';
  31. //方法扩展
  32. public function write1(){
  33. return parent::write().",职业:{$this->profession}。";
  34. }
  35. }
  36. $user = new User;
  37. echo $user->write().'<br>';
  38. $rob = new Rob;
  39. echo $rob->write().'<br>';
  40. echo $rob->write1();

2.抽象类的作用与实现

  1. //抽象类:部分分离了“设计与实现” abstract关键字
  2. //抽象类不能实例化
  3. abstract class User
  4. {
  5. protected $name = '张三';
  6. protected $age = 40;
  7. //方法有方法体
  8. public function write()
  9. {
  10. return "姓名:{$this->name},年龄:{$this->age}";
  11. }
  12. //没有方法体
  13. abstract protected function write1();
  14. }
  15. class Rob extends User{
  16. protected $profession = '普通职员';
  17. public function write1()
  18. {
  19. return parent::write()."职业:{$this->profession}";
  20. }
  21. }
  22. // $user = new User;
  23. // echo $user->write();
  24. $rob = new Rob;
  25. echo $rob->write1();

3.接口

  1. //接口:完全分离了设计与实现
  2. //关键字:interface
  3. //使用与类相似的语法:抽象方法,常量,构造方法
  4. //默认访问控制必须是public
  5. //1.单接口
  6. interface iUser
  7. {
  8. //接口常量
  9. const NATION = '中国';
  10. //接口方法
  11. public static function write();
  12. }
  13. //实现接口
  14. class User implements iUser
  15. {
  16. //接口中的抽象方法,必须在工作类中实现
  17. protected static $name ='张三';
  18. public static function write()
  19. {
  20. return self::$name.'的国籍是:'.iUser::NATION;
  21. }
  22. }
  23. //接口常量的访问与类常量一样 如果不想实例化那就把方法设置成静态方法
  24. echo User::write();

总结

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

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!