zoukankan      html  css  js  c++  java
  • 复习重载

    昨天复习了类的继承,今天复习一下重载。

    PHP如果不每天练习,忘记的速度很快。

     1 <?php
     2   class student
     3   {
     4       var $no;
     5       var $name;
     6       var $gender;
     7       var $age;
     8       
     9       function set_data($arr)
    10       {
    11           $this->no=$arr["no"];
    12           $this->name=$arr["name"];
    13           $this->gender=$arr["gender"];
    14           $this->age=$arr["age"];
    15           }
    16           
    17           function grow($i){
    18               $this->age+=$i;
    19               }
    20       function get_data(){
    21           echo"<br />
    22   <b>学生信息</b><br />
    23   ";
    24   echo "学号:$this->no<br />
    25   ";echo "姓名:$this->name<br />
    26   ";echo "性别:$this->gender<br />
    27   ";echo "年龄:$this->age<br />
    28   ";
    29           }
    30       }
    31   
    32  class college_student extends student{
    33      var $department;
    34  
    35      function change_department($new_department){
    36          $this->department=$new_department;
    37          }
    38          
    39      function set_data($arr)
    40       {
    41           $this->no=$arr["no"];
    42           $this->name=$arr["name"];
    43           $this->gender=$arr["gender"];
    44           $this->age=$arr["age"];
    45           $this->department=$arr["department"];}     
    46     
    47      } 
    48  $s=new college_student;
    49       $temparr=array("no"=>"001","name"=>"lisa","gender"=>"male","age"=>"22","department"=>"计算机系");
    50  
    51   $s->set_data($temparr);
    52 
    53 
    54   echo "$s->name"."的所在系是$s->department<br />";
    55  $s->change_department("数学系");
    56    echo "$s->name"."的所在系是$s->department<br />";
    57      
    58      
    59     ?>

    运行结果:

    lisa的所在系是计算机系
    lisa的所在系是数学系

    这段代码很简单,重点是要理解“重载”,在PHP里,所谓重载,就是在子类中,修改了从父类继承过来的方法。

    而之前的构造函数,是指在类中,创建的函数名与类名相同函数。

  • 相关阅读:
    SQL Server 中的事务与事务隔离级别以及如何理解脏读, 未提交读,不可重复读和幻读产生的过程和原因
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSAS 系列
    微软BI 之SSRS 系列
    微软BI 之SSRS 系列
    配置 SQL Server Email 发送以及 Job 的 Notification通知功能
  • 原文地址:https://www.cnblogs.com/4php/p/2800582.html
Copyright © 2011-2022 走看看