登录  /  注册
博主信息
博文 61
粉丝 1
评论 0
访问量 68148
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
0613-PHP类构造方法、扩展和封装
我的博客
原创
1254人浏览过

实例

<?php
//1、创建一个对象的模板-----没有实例化
   //$this :谁实例化类,就是谁,他是一个对象,比如$student =new students(),%this指的是$student
   //parent :是指向父类的指针,一般我们使用parent来调用父类的构造函数。

class Students
{
    //属性
     public $name,$age,$gender,$bhw;

    //方法
    public function hobby($hobby){
        return '他爱好是' . $hobby;
    }

}
// 实例化,并赋值,可以直接赋值在字符串拼接里面

$student = new Students();   //实例化必须有
//$student->name = '小明';
//$student->age = 18;
//$student->gender = '男';
//$student->bhw = [10,20,30];
//$student->hobby('吃***');

//访问,字符串拼接
$res = '学生信息';
$res .= '<br>姓名:'.$student->name = '小明';   //第一个不用 .= 拼接,直接等号就可以
$res .= '<br>年龄:'.$student->age=18;
$res .= '<br>性别:'.$student->gender='男';
$res .= '<br>三围:'.var_export($student->bhw = [10,20,30],true);
$res .= '<br>爱好:'.$student->hobby('吃***');
echo $res;

echo '<hr>';
// 构造方法  ----  __construct
class Students_1
{
    //属性
    public $name,$age,$gender,$bhw;

    //构造方法  __construct:调用立即执行
    public function __construct($name,$age,$gender,$bhw)
    {
        $this->name=$name;
        $this->age=$age;
        $this->gender=$gender;
        $this->bhw=$bhw;

      // echo $this->getInfo();  //直接运行这里调用打印函数。
    }

    public function hobby($hobby){
        return '他爱好是' . $hobby;  //函数必须返回数值
    }


    public function getInfo(){
        $res = '父函数:学生信息';
        $res .= '<br>姓名:'. $this->name;   //第一个不用 .= 拼接,直接等号就可以
        $res .= '<br>年龄:'.$this->age;
        $res .= '<br>性别:'.$this->gender;
        $res .= '<br>三围:'.print_r($this->bhw,true);
        $res .= '<br>爱好:'.$this->hobby('吃牛粪');
        $res .='<br>-----分割线-----';
        return $res;  //必须给给类返回数值,不然$this->getInfo不能调用

    }
}

$student1 = new Students_1('笑二','19','女',[20,20,20]);
//echo '<br>单独调用类里面的hobby函数:';
//echo $student1->hobby('喝酒');
//单独输出getInfo()
echo $student1->getInfo();

echo '<hr>';

//类的功能扩展:继承(继承一下class Students_2)
    // 被扩展的类叫父类, 超类,或基类
    // 扩展的类叫子类, 派生类
    // 在扩展子类中, 父类中的public属性成员,可以直接使用
    // 如果仅是简单的继承, 子类并没有添加新的属性或方法, 就失去了继承的意义
    // 继承的本质是代码复用
class SubStudents_1 extends Students_1
{
    private $ac;   //添加一个新属性:成绩

    public function __construct($name,$age,$gender,$bhw,$ac)
    {
//        $this->name=$name;
//        $this->age=$age;
//        $this->gender=$gender;
//        $this->bhw=$bhw;

        //用parent 继承父类__construct的所有参数和内容,包括输出函数echo this->getInfo()
        parent::__construct($name,$age,$gender,$bhw);
        //增加一个学习成绩

          $this->ac=$ac;
        echo $this->getInfo();  //父函数和子函数只能有一个getInfo(),主函数里有经过继承就会运行主函数里面的getInfo()


    }

    public function hobby($hobby){
        return '他爱好是' . $hobby;  //函数必须返回数值
    }
    //方法的扩展叫方法重写,必须重写,不能通过parent继承内容
    public function getInfo(){
        $res = '扩展函数:学生信息';
        $res .= '<br>姓名:'. $this->name;   //第一个不用 .= 拼接,直接等号就可以
        $res .= '<br>年龄:'.$this->age;
        $res .= '<br>性别:'.$this->gender;
        $res .= '<br>三围:'.print_r($this->bhw,true);
        $res .= '<br>爱好:'.$this->hobby('吃肉');
        $res .= '<br>成绩:'.$this->ac ;

        return $res;  //必须给给类返回数值,不然$this->getInfo不能调用
   }

}

$student2 = new SubStudents_1('马东明','29','未知',[31,22,60],48);

echo '<hr>';


//封装技术
   //将  public(公众属性) 改成 private(私有属性) 或者 protected(局部属性)
   //先简单粗俗的描述下:
   //public 表示全局,类内部外部子类都可以访问;
   //private表示私有的,只有本类内部可以使用;
   //protected表示受保护的,只有本类或子类或父类中可以访问;
class Students_2
{
    //属性
    public $name;
    protected $age;
    protected $bhw;
    protected $gender;
    //构造方法  __construct:调用立即执行
    public function __construct($name,$age,$gender,$bhw)
    {   //不想类外部查看三围 $bhw,设置$bhw为 private
        $this->name=$name;
        $this->age=$age;
        $this->gender=$gender;
        $this->bhw=$bhw;

         echo $this->getInfo();  //直接运行这里调用打印函数。
    }

    public function hobby($hobby){
        return '他爱好是' . $hobby;  //函数必须返回数值
    }


    public function getInfo(){
        $res = '父函数:学生信息';
        $res .= '<br>姓名:'. $this->name;   //第一个不用 .= 拼接,直接等号就可以
        $res .= '<br>年龄:'.$this->age;
        $res .= '<br>性别:'.$this->gender;
        $res .= '<br>三围:'.print_r($this->bhw,true);
        $res .= '<br>爱好:'.$this->hobby('吃牛粪');
        $res .='<br>';
        return $res;  //必须给给类返回数值,不然$this->getInfo不能调用
    }
    //想在外部访问类属性或函数,必须给他用函数做一个接口,来供外部访问
        public function getsw(){

        return  print_r($this->bhw,true);
    }

}

$students_2 = new Students_2('马冬梅','5','女',[88,77,50]);
echo $students_2->name . '<br>';
//访问不了 私有变量  echo $students_2->bhw;
//也访问不了受保护类型变量 echo $students_2->gender


//外部访问getsw()函数,public getsw()是全局属性,所以可以通过它间接访问$bhw三围数据。
echo '三围属性是:'.$students_2->getsw().'<hr>';

//扩展上面的类,测试protected 使用方法
class subStudents_2 extends Students_2
{
    //添加一个属性:漂亮程度和ID
    private $id;
    private $bui;
    //父函数$age使用protected ,看这里是否能正常使用?看似有变量$bhw是否正常使用

    public function __construct($name, $age, $gender, $bhw,$bui,$id)
    {

        parent::__construct($name, $age, $gender, $bhw);
        $this->bui=$bui;
        $this->id = $id;
        //子类不能使用父类private 定义的$bhw
        //$this->bhw=$bhw;
        echo $this->getsw();

    }
        //重写获取三围函数
        public function getsw(){

            if ($this->id=== 001){
                echo $this->name.'美丽度是:'.$this->bui;
            }else{echo '你不是大象管理员,禁止观看大象美丽度';}


    }
}
//用变量表示也行

$qq = [
    'id'=>001,
    'name'=>'大象',
    'age'=>88,
    'gender'=>'公',
    'bhw' => [77,88,99],
    'bui' => 'AAAAA'
];
$subs_2 = new subStudents_2($qq['name'],$qq['age'],$qq['gender'],
    $qq['bhw'],$qq['bui'],$qq['id']);

运行实例 »

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


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学