博客列表 >类与对象基础知识_2018-09_03:19:38上传

类与对象基础知识_2018-09_03:19:38上传

斯达融的博客
斯达融的博客原创
2018年09月02日 19:45:25628浏览

一、类是抽象出来的,对象是具体的;对象是类的实例。

二、练习代码:

1、写了一个用来描述学生的类:student.

实例
<?php
class Student{
 //学生类
 public $name='';
 public $cardid='';
 public $cardtype='';
 public $schoolid='';
 public $xjcard='';
 public $birthday='';
 public $bornwhere='';
 public $buji='';
 public $nation='';
 public $guoji='';
 public $health='';
 public $hukoutype='';
 public $hujiwhere='';
 public $bjcode='';
 public $bj='';
 public $indate='';
 public $intype='';
 public $studytype='';
 public $address='';
 public $tel='';
 public $is_onlychild=1;
 public $is_stayhome=1;
 public $is_help=0;
 public $is_helped=0;
 public $is_ykd=1;
 public $alone=0;
 public $schoolcode='';
 public $member1_cardid='';
 public $memner2_cardid='';
 //构造器
 public function __construct(array $stuinfo)
 {

        $this->name=array_key_exists('name',$stuinfo)?$stuinfo['name']:'';
        $this->cardid=array_key_exists('cardid',$stuinfo)?$stuinfo['cardid']:'';
        $this->cardtype=array_key_exists('cardtype',$stuinfo)?$stuinfo['cardtype']:'';
        $this->schoolid=array_key_exists('schoolid',$stuinfo)?$stuinfo['schoolid']:'';
        $this->xjcard=array_key_exists('xjcard',$stuinfo)?$stuinfo['xjcard']:'';
        $this->birthday=array_key_exists('birthday',$stuinfo)?$stuinfo['birthday']:'';
        $this->bornwhere=array_key_exists('bornwhere',$stuinfo)?$stuinfo['bornwhere']:'';
        $this->huji=array_key_exists('huji',$stuinfo)?$stuinfo['huji']:'';
        $this->nation=array_key_exists('nation',$stuinfo)?$stuinfo['nation']:'汉族';
        $this->guoji=array_key_exists('guoji',$stuinfo)?$stuinfo['guoji']:'中国';
        $this->health=array_key_exists('health',$stuinfo)?$stuinfo['health']:'';
        $this->hujiwhere=array_key_exists('hujiwhere',$stuinfo)?$stuinfo['hujiwhere']:'';
        $this->hukoutype=array_key_exists('hukoutype',$stuinfo)?$stuinfo['hukoutype']:'非农业户口';
        $this->bj=array_key_exists('bj',$stuinfo)?$stuinfo['bj']:'';
        $this->bjcode=array_key_exists('bjcode',$stuinfo)?$stuinfo['bjcode']:'';
        $this->indate=array_key_exists('indate',$stuinfo)?$stuinfo['indate']:'';
        $this->intype=array_key_exists('intype',$stuinfo)?$stuinfo['intype']:'';
        $this->studytype=array_key_exists('studytype',$stuinfo)?$stuinfo['studytype']:'走读';
        $this->address=array_key_exists('address',$stuinfo)?$stuinfo['address']:'';
        $this->tel=array_key_exists('tel',$stuinfo)?$stuinfo['tel']:'';
        $this->is_onlychild=array_key_exists('is_onlychild',$stuinfo)?$stuinfo['is_onlychild']:1;
        $this->is_stayhome=array_key_exists('is_stayhome',$stuinfo)?$stuinfo['is_stayhome']:0;
        $this->is_ykd=array_key_exists('is_ykd',$stuinfo)?$stuinfo['is_ykd']:1;
        $this->is_help=array_key_exists('is_help',$stuinfo)?$stuinfo['is_help']:0;
        $this->is_helped=array_key_exists('is_helped',$stuinfo)?$stuinfo['is_helped']:0;
        $this->is_alone=array_key_exists('is_alone',$stuinfo)?$stuinfo['is_alone']:0;
        $this->schoolcode=array_key_exists('schoolcode',$stuinfo)?$stuinfo['schoolcode']:'';
        $this->member1_cardid=array_key_exists('member1_cardid',$stuinfo)?$stuinfo['member1_cardid']:'';
        $this->member2_cardid=array_key_exists('member2_cardid',$stuinfo)?$stuinfo['member2_cardid']:'';






 }


//获取器
 public function __get($name)
 {
     $msg = null;
     if (isset($this->$name)) {
         $msg = $this->$name;
     } elseif (isset($this->data[$name])) {
         $msg = $this->data[$name];
     } else {
         $msg = '无此属性';
     }
     return $msg;
 }

 //设置器
 public function __set($name, $value)
 {
     $this->$name = $value;
 }




}
 ?>

运行实例 »
点击 "运行实例" 按钮查看在线实例

2.测试类的操作:初始化,更改和获取公共属性。

实例

<?php
require 'Student.php';
$stuinfo=['name'=>'张三','cardid'=>'420528200709080023','bj'=>'1801','hujiwhere'=>'湖北宜昌伍家岗','member1_cardid'=>'427232197208090021'];
$stu=new Student($stuinfo);//初始化
echo $stu->name,"</br>",$stu->cardid,"</br>",$stu->bj,"</br>",$stu->hujiwhere,"</br>",$stu->member1_cardid,"<br>";
//更改属性值
$stu->name='李四';
echo $stu->name,"<hr>";

 ?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

三、总结:这部分知识早己掌握,只是在项目实施时,还是未能完全按照面向对象的思想来实施。需要进一步学习。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议