PHP类继承实例: 高中学生 -- 学生--人

PHP 类的继承(extends)

 

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

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

 

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

 

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

 

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

 

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

 


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

 

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

 

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

 

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

 

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

 

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

 

Student类继承了Person类,继承使用关键字extends;

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

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

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

测试页面为extends.php,实现如下所示:

 

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

 

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

 

$student->display();

 

$student->enroll($student->getScore());

 

unset($student);
?>

 

启动Apache服务器,在IE地址栏中键入:

http://localhost/shirdrn/oo/extends.php

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

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

Person类的实现为person.inc,如下所示:

 

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

 

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

 

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

 

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

 

Student类的实现为student.inc,如下所示:

 

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

 

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

 

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

 

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

 

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

 

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

 

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

require("student.inc");

实现代码如下所示:

 

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

 

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

 

$student->display();

 

$student->enroll($student->getScore());

 

unset($student);
?>

 

启动Apache服务器,在IE地址栏中键入:

http://localhost/shirdrn/oo/ext.php

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



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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值