Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239974
  • 博文数量: 108
  • 博客积分: 3045
  • 博客等级: 中校
  • 技术积分: 1162
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-19 18:26
文章分类

全部博文(108)

分类: PHP

2013-07-23 21:05:18


  1. <?php

  2. /* 类的方法的重载
  3.  * 方法的覆盖
  4.  * 重写父类的方法
  5.  * ::范围解析操作符(也可称作 Paamayim Nekudotayim)或者更简单地说是一对冒号,可以用于访问静态成员、方法和常量,还可以用于覆盖类中的成员和方法。
  6.  *当在类的外部访问这些静态成员、方法和常量时,必须使用类的名字。
  7.  *
  8.  *访问类型private public protected:在子类中可用,不可以在外部使用
  9.  */
  10. class Person

  11. {
  12.     
  13.     // 成员属性

  14.     protected $name;
  15.     protected $age; // 私人有属性

  16.     protected $sex;
  17.     
  18.     // 构造方法

  19.     function __construct($name, $age, $sex)     // php5申明方法 如果两个构造方法同时存在,先找php5.

  20.     
  21.     {
  22.         $this->name = $name;
  23.         
  24.         $this->age = $age;
  25.         
  26.         $this->sex = $sex;
  27.     }
  28.     
  29.     // 成员方法

  30.     function say()

  31.     {
  32.         echo "我的名字是: " . $this->name . " 年龄:" . $this->age . " 性别:" . $this->sex . "
    "
    ;
  33.         
  34.     }
  35.     

  36.     protected function run()

  37.     {
  38.         echo "我在走路";
  39.     }
  40.     private function eat()

  41.     {
  42.         echo $this->name . "在吃饭
    "
    ;
  43.     }
  44.     function __destruct()

  45.     {
  46.         echo $this->name . " 再见!!!
    "
    ;
  47.     }
  48. }

  49. class student extends Person
  50. {
  51.     var $school;
  52.     
  53.     function __construct($name="", $age="", $sex="",$school="")
  54.     {
  55.         parent::__construct($name, $age, $sex);
  56.         $this->school=$school;
  57.     }
  58.     
  59.     function study()
  60.     {
  61.         $this->run();
  62.     }
  63.     
  64.     function say()
  65.     {
  66.         //echo "我的名字是: " . $this->name . " 年龄:" . $this->age . " 性别:" . $this->sex . "
    ";

  67.         parent::say();
  68.         echo "我所在的学校是:" .$this->school."
    "
    ;
  69.     }
  70. }


  71. class Teacher extends student
  72. {

  73. }

  74. $s1=new student("张三", 23, "男","北京大学");

  75. $s1->say();

  76. $s1->study();


  77. ?>

阅读(279) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~