---
title: "PHP类继承实例: 高中学生 -- 学生--人"
author: "yd514"
date: "2013-05-10T10:52:55Z"
tags: []
categories: ["编程语言"]
slug: "php"
draft: false
lastmod: "2013-05-10T10:56:33Z"
id: 1866622
url: "https://yd514.iteye.com/blog/1866622"
---

## PHP 类的继承(extends)

 

编写了两个文件：person-student.inc和extends.php。

person-student.inc是一个类文件，包含了对一个Person的定义，其中Student类继承了Person类，实现如下所示：

 

&lt;?php  
class Person{  
   var $id;  
   var $name;  
   var $age;  
   var $email;  
   function Person($id,$name,$age,$email){  
    $this-&gt;id = $id;  
    $this-&gt;name = $name;  
    $this-&gt;age = $age;  
    $this-&gt;email = $email;  
   }

 

   function setId($id){  
    $this-&gt;id = $id;  
   }  
   function setName($name){  
    $this-&gt;name = $name;  
   }  
   function setAge($age){  
    $this-&gt;age = $age;  
   }  
   function setEmail($email){  
    $this-&gt;email = $email;  
   }

 

   function getId(){  
    return $this-&gt;id;  
   }  
   function getName(){  
    return $this-&gt;name;  
   }  
   function getAge(){  
    return $this-&gt;age;  
   }  
   function getEmail(){  
    return $this-&gt;email;  
   }

 

   function display(){  
    echo "&lt;B&gt;身份证号：&lt;/B&gt;".$this-&gt;id."&lt;BR&gt;";  
    echo "&lt;B&gt;真实姓名：&lt;/B&gt;".$this-&gt;name."&lt;BR&gt;";  
    echo "&lt;B&gt;实际年龄：&lt;/B&gt;".$this-&gt;age."&lt;BR&gt;";  
    echo "&lt;B&gt;联系邮箱：&lt;/B&gt;".$this-&gt;email."&lt;BR&gt;";  
    echo "&lt;BR&gt;";  
   }  
}

 

class Student extends Person{  
   var $sno;  
   var $school;  
   var $grade;  
   var $score;

 

   function Student($sno,$name,$school,$grade,$score){  
    $this-&gt;sno = $sno;  
    $this-&gt;name = $name;  
    $this-&gt;school = $school;  
    $this-&gt;grade = $grade;  
    $this-&gt;score = $score;  
   }

 

   function setSno($sno){  
    $this-&gt;sno = $sno;  
   }  
   function setSchool($school){  
    $this-&gt;school = $school;  
   }  
   function setGrade($grade){  
    $this-&gt;grade = $grade;  
   }  
   function setScore($score){  
    $this-&gt;scorer = $score;  
   }

 

   function getSno(){  
    return $this-&gt;sno;  
   }  
   function getSchool(){  
    return $this-&gt;school;  
   }  
   function getGrade(){  
    return $this-&gt;grade;  
   }  
   function getScore(){  
    return $this-&gt;score;  
   }

 

   function display(){  
    echo "&lt;B&gt;学生个人信息如下：&lt;/B&gt;&lt;BR&gt;&lt;BR&gt;";  
    parent::display();  
    echo "&lt;B&gt;学生学号：&lt;/B&gt;".$this-&gt;sno."&lt;BR&gt;";  
    echo "&lt;B&gt;所在学校：&lt;/B&gt;".$this-&gt;school."&lt;BR&gt;";  
    echo "&lt;B&gt;所在年级：&lt;/B&gt;".$this-&gt;grade."&lt;BR&gt;";  
    echo "&lt;B&gt;高考分数：&lt;/B&gt;".$this-&gt;score."&lt;BR&gt;";  
    echo "&lt;BR&gt;";  
   }

 

   function enroll($score){  
    $flag = floor($score/100);  
    echo "&lt;B&gt;你的考试录取估计如下：&lt;/B&gt;&lt;BR&gt;";  
    switch($flag){  
     case 7:  
     case 6:  
      echo "你的分数为".$score."，好强啊，能够考取一个名牌大学。&lt;BR&gt;";  
      break;  
     case 5:  
      echo "你的分数为".$score."，能够考取一个普通重点大学。&lt;BR&gt;";  
      break;  
     case 4:  
      echo "你的分数为".$score."，能够考取一个普通本科大学。&lt;BR&gt;";  
      break;  
     case 3:  
      echo "你的分数为".$score."，能够考取一个专科大学。&lt;BR&gt;";  
      break;  
     default:  
      echo "你的分数也太低了点，继续努力吧。&lt;BR&gt;";  
      break;  
      
    }  
   }  
}  
?&gt;

 

Student类继承了Person类，继承使用关键字extends；

上面Student类又新增了一些独有的成员变量；

同 时Student类重写了Person类中的同名方法display()方法，在Student类中该方法使用parent关键字调用了基类的 display()方法，调用基类方法使用操作符“::”，当然也可以使用类名来调用(也就是说，parent::display();与 Person::display();是等价的)；

Student类又增加了一个enroll()方法的实现。

测试页面为extends.php，实现如下所示：

 

&lt;?php  
require("person-student.inc");  
$student = new Student(2008001,"Shinrdrn","长春28中","高三",666);

 

$student-&gt;setId("2224031983011999x");  
$student-&gt;setAge(26);  
$student-&gt;setEmail("");

 

$student-&gt;display();

 

$student-&gt;enroll($student-&gt;getScore());

 

unset($student);  
?&gt;

 

启动Apache服务器，在IE地址栏中键入：

[http://localhost/shirdrn/oo/extends.php](http://localhost/shirdrn/oo/extends.php)

浏览页面显示结果如下所示：

![](http://hiphotos.baidu.com/shirdrn/pic/item/e5df2964007924e8f73654e4.jpg)

上面的实现，是将Person类和Student类写在了同一个类文件中，当然也可以将他们分开，每一个类写在一个单独的类文件中，下面使用两个类文件分别实现Person类和Student。

Person类的实现为person.inc，如下所示：

 

&lt;?php  
class Person{  
   var $id;  
   var $name;  
   var $age;  
   var $email;  
   function Person($id,$name,$age,$email){  
    $this-&gt;id = $id;  
    $this-&gt;name = $name;  
    $this-&gt;age = $age;  
    $this-&gt;email = $email;  
   }

 

   function setId($id){  
    $this-&gt;id = $id;  
   }  
   function setName($name){  
    $this-&gt;name = $name;  
   }  
   function setAge($age){  
    $this-&gt;age = $age;  
   }  
   function setEmail($email){  
    $this-&gt;email = $email;  
   }

 

   function getId(){  
    return $this-&gt;id;  
   }  
   function getName(){  
    return $this-&gt;name;  
   }  
   function getAge(){  
    return $this-&gt;age;  
   }  
   function getEmail(){  
    return $this-&gt;email;  
   }

 

   function display(){  
    echo "&lt;B&gt;身份证号：&lt;/B&gt;".$this-&gt;id."&lt;BR&gt;";  
    echo "&lt;B&gt;真实姓名：&lt;/B&gt;".$this-&gt;name."&lt;BR&gt;";  
    echo "&lt;B&gt;实际年龄：&lt;/B&gt;".$this-&gt;age."&lt;BR&gt;";  
    echo "&lt;B&gt;联系邮箱：&lt;/B&gt;".$this-&gt;email."&lt;BR&gt;";  
    echo "&lt;BR&gt;";  
   }  
}  
?&gt;

 

Student类的实现为student.inc，如下所示：

 

&lt;?php  
require("person.inc");  
class Student extends Person{  
   var $sno;  
   var $school;  
   var $grade;  
   var $score;

 

   function Student($sno,$name,$school,$grade,$score){  
    $this-&gt;sno = $sno;  
    $this-&gt;name = $name;  
    $this-&gt;school = $school;  
    $this-&gt;grade = $grade;  
    $this-&gt;score = $score;  
   }

 

   function setSno($sno){  
    $this-&gt;sno = $sno;  
   }  
   function setSchool($school){  
    $this-&gt;school = $school;  
   }  
   function setGrade($grade){  
    $this-&gt;grade = $grade;  
   }  
   function setScore($score){  
    $this-&gt;scorer = $score;  
   }

 

   function getSno(){  
    return $this-&gt;sno;  
   }  
   function getSchool(){  
    return $this-&gt;school;  
   }  
   function getGrade(){  
    return $this-&gt;grade;  
   }  
   function getScore(){  
    return $this-&gt;score;  
   }

 

   function display(){  
    echo "&lt;B&gt;学生个人信息如下：&lt;/B&gt;&lt;BR&gt;&lt;BR&gt;";  
    parent::display();  
    echo "&lt;B&gt;学生学号：&lt;/B&gt;".$this-&gt;sno."&lt;BR&gt;";  
    echo "&lt;B&gt;所在学校：&lt;/B&gt;".$this-&gt;school."&lt;BR&gt;";  
    echo "&lt;B&gt;所在年级：&lt;/B&gt;".$this-&gt;grade."&lt;BR&gt;";  
    echo "&lt;B&gt;高考分数：&lt;/B&gt;".$this-&gt;score."&lt;BR&gt;";  
    echo "&lt;BR&gt;";  
   }

 

   function enroll($score){  
    $flag = floor($score/100);  
    echo "&lt;B&gt;你的考试录取估计如下：&lt;/B&gt;&lt;BR&gt;";  
    switch($flag){  
     case 7:  
     case 6:  
      echo "你的分数为".$score."，好强啊，能够考取一个名牌大学。&lt;BR&gt;";  
      break;  
     case 5:  
      echo "你的分数为".$score."，能够考取一个普通重点大学。&lt;BR&gt;";  
      break;  
     case 4:  
      echo "你的分数为".$score."，能够考取一个普通本科大学。&lt;BR&gt;";  
      break;  
     case 3:  
      echo "你的分数为".$score."，能够考取一个专科大学。&lt;BR&gt;";  
      break;  
     default:  
      echo "你的分数也太低了点，继续努力吧。&lt;BR&gt;";  
      break;  
      
    }  
   }  
}  
?&gt;

 

测试文件为ext.php，在该文件中使用Student类只需要包含student.inc文件：

require("student.inc");

实现代码如下所示：

 

&lt;?php  
require("student.inc");  
$student = new Student(2008001,"Zhangsan","XX重点中学","高三",260);

 

$student-&gt;setId("5004031988011999x");  
$student-&gt;setAge(22);  
$student-&gt;setEmail("");

 

$student-&gt;display();

 

$student-&gt;enroll($student-&gt;getScore());

 

unset($student);  
?&gt;

 

启动Apache服务器，在IE地址栏中键入：

[http://localhost/shirdrn/oo/ext.php](http://localhost/shirdrn/oo/ext.php)

浏览页面显示结果如下所示：

![](http://hiphotos.baidu.com/shirdrn/pic/item/926616ee537466312df534d2.jpg)

来源：http://hi.baidu.com/shirdrn/item/19b0a6b152894476254b09a9