找回密码
 立即注册
搜索
查看: 1838|回复: 0

[面向对象] 【OOP代码示例】继承性

[复制链接]

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

发表于 2016-1-27 17:50:59 | 显示全部楼层 |阅读模式
类继承的应用
  1. <?php
  2. class person{
  3.     public $name;
  4.     public $age;
  5.     public $sex;

  6.     public function __construct($name, $age, $sex){
  7.         $this -> name = $name;
  8.         $this -> age = $age;
  9.         $this -> sex = $sex;
  10.     }

  11.     public function say(){
  12.         echo "say...";
  13.     }

  14.     public function eat(){
  15.         echo "eat...";
  16.     }

  17.     public function run(){
  18.         echo "run...";
  19.     }
  20. }

  21. class teacher extends person{

  22.     public function teach(){
  23.         echo "teach...";
  24.     }
  25. }

  26. class student extends person{
  27.    
  28.     public function learn(){
  29.         echo "learn...";
  30.     }
  31. }


  32. $teacher = new teacher("zhangsan",30,'nan');
  33. //$teacher -> say();
  34. $teacher -> teach();
  35. $teacher -> learn();
  36. echo "<hr />";
  37. $student = new student("lisi",18,'nv');
  38. //$student -> run();
  39. $student -> learn();
复制代码
  1. <?php
  2. //PHP是单继承 一个类只能有一个父类
  3. //一个类可以有多个子类
  4. //支持多层继承
  5. class A{

  6.     public $name = "zhangshan";
  7.     public $age = 20;

  8.     public function say(){
  9.         return $this -> name;
  10.     }
  11. }

  12. class B extends A{

  13. }

  14. class C extends B{

  15. }

  16. $b = new B();
  17. var_dump($b);
  18. echo $b -> say();
  19. echo "<hr />";
  20. $c = new C();
  21. var_dump($c);
  22. echo $c -> say();

复制代码
访问类型的控制

  1. <?php

  2. class person{
  3.     public $name;
  4.     private $age;
  5.     protected $sex;

  6.     public function __construct($name,$age,$sex){
  7.         $this -> name = $name;
  8.         $this -> age = $age;
  9.         $this -> sex = $sex;
  10.     }

  11.     public function p1(){
  12.         echo "p1";
  13.     }

  14.     private function p2(){
  15.         echo "p2";
  16.     }

  17.     protected function p3(){
  18.         echo "p3";
  19.     }

  20.     public function test1(){
  21.         //echo $this -> name;               //公有的成员属性在类的内部可以访问   
  22.         //echo $this -> age;                //私有的成员属性在类的内部可以访问
  23.         //echo $this -> sex;                //受保护的成员属性在类的内部可以访问
  24.         //$this -> p1();                    //公有的成员方法在类的内部可以访问
  25.         //$this -> p2();                    //私有的成员方法在类的内部可以访问
  26.         //$this -> p3();                    //受保护的成员方法在类的内部可以访问
  27.     }
  28. }

  29. class student extends person{

  30.     public function test(){
  31.         // echo $this -> name;          //公有的成员属性在子类中可以访问
  32.         //echo $this -> age;            //私有的成员属性在子类中不可以访问           
  33.         //echo $this -> sex;            //受保护的成员属性在子类中可以访问
  34.         //$this -> p1();                //公有的成员方法在子类中可以访问
  35.         //$this -> p2();                //私有的成员方法在子类中不可以访问
  36.         //$this -> p3();                //受保护的成员方法在了类中可以访问
  37.     }
  38. }


  39. $person = new person("zhangsan",18,"nan");
  40. //echo $person -> name;         //公有的成员属性在类的外部可以访问
  41. //echo $person -> age;          //私有的成员属性在类的外部不可以直接访问
  42. //echo $person -> sex;          //受保护的成员属性在类的外部不可以直接访问
  43. //$person -> p1();              //公有的成员方法在类的外部可以访问
  44. //$person -> p2();              //私的有成员方法在类的外部不可以直接访问
  45. //$person -> p3();              //受保护的成员方法在类的外部不可以直接访问
  46. //
  47. //$student = new student("lisi",20,"nv");
  48. //$student -> test();
  49. //
  50. $person -> test1();
复制代码
子类中重载父类方法

  1. <?php
  2. class person{
  3.     public $name;
  4.     public $age;
  5.     public $sex;

  6.     public function __construct($name, $age, $sex){
  7.         $this -> name = $name;
  8.         $this -> age = $age;
  9.         $this -> sex = $sex;
  10.     }

  11.     public function say(){
  12.         echo "我的名字是:{$this -> name},我的年龄是:{$this -> age},我的性别是:{$this -> sex}";
  13.     }
  14. }

  15. class teacher extends person{
  16.     public $teach;

  17.     public function __construct($name, $age, $sex, $teach){
  18.         parent::__construct($name, $age, $sex);
  19.         $this -> teach = $teach;
  20.     }

  21.     //重写:就是声明 一个与父类中同名的方法
  22.     public function say(){
  23.         //重载:parent::父类中的方法名
  24.         parent::say();
  25.         echo ",我教的学科是:{$this -> teach}";
  26.     }
  27. }

  28. class student extends person{
  29.     public $school;

  30.     public function __construct($name, $age, $sex, $school){
  31.         parent::__construct($name, $age, $sex);
  32.         $this -> school = $school;
  33.     }

  34.     public function say(){
  35.         parent::say();
  36.         echo ",我所在的学校是:{$this -> school}";
  37.     }
  38. }

  39. $teacher = new teacher("张三", 30, '男', '数学');
  40. $teacher -> say();

  41. echo "<hr />";
  42. $student = new student("李四", 18, '女', '北大');
  43. $student -> say();
复制代码






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|十三博客 ( 鲁ICP备2023000528号 )

GMT+8, 2026-6-1 17:28 , Processed in 0.049834 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表