设为首页 收藏本站
查看: 296|回复: 0

[经验分享] PHP类继承实例: 高中学生 -- 学生--人

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-4-8 13:35:36 | 显示全部楼层 |阅读模式
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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362030-1-1.html 上篇帖子: PHPExcel:用于Excel等文档生成的PHP开源类库 下篇帖子: php缓冲器:eaccelerator与xcache性能测试对比
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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