登录  /  注册
博主信息
博文 31
粉丝 0
评论 2
访问量 26523
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
5月3日作业 —— 父类与子类
钱光照的博客
原创
951人浏览过

一个父类与子类,在子类中扩展父类的属性,并重载父类的方法的案例:

1、父类代码class/Family.php:

实例

<?php

/**
 * 创建 Family类
 */
define('FAMILY_NAME','温馨之家');
class Family
{
    protected $familyName= FAMILY_NAME;
    protected $familyAddr = '安徽省无为县严桥镇';
    protected $postCodes = '238381';
    const COMPANY = '严桥中学';


    //构造方法
    public function __construct($familyName='',$familyAddr='', $postCodes='') 
    {
        //如果传参,则使用新值初始化属性,否则使用默认值
        $familyName ? ($this->familyName = $familyName) : $this->familyName;
        $familyAddr ? ($this->familyAddr = $familyAddr) : $this->familyAddr;
        $postCodes ? ($this->postCodes = $postCodes) : $this->postCodes;                
    }
    
    //查询器
    public function __get($attribute)
    {
        return $this->$attribute;
    }
    
    //设置器
    public function __set($attribute,$value)
    {
        return $this->$attribute = $value;
    }
    
    //在类中访问类常量,使用self来引用当前类名
    public function getConst()
    {
        //类内部也可以直接使用当前类名
//        return Family::COMPANY;
        //推荐使用self:当类名改变时,不必修改内部对它的引用
        return self::COMPANY;
    }
    public function where(){
    return $this->familyAddr;
    }
}

运行实例 »

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


2、子类代码class/Person1.php:

实例

<?php

/**
 * 创建个人类:Person1
 */
class Person1 extends Family
{
     //属性声明
    private $name='';// 姓名
    private $age=0;// 年龄
    private $hobby=[];// 爱好
    private $relation='';// 关系
    private $data=[];//自定义数据收集器


    //构造方法
    public function __construct($name='',$age=0,array $hobby=[],$relation='',$familyName = '', $familyAddr = '', $postCodes = '')
    {
        parent::__construct($familyName, $familyAddr, $postCodes);
        $this->name=$name;
        $this->age=$age;
        $this->hobby=$hobby;
        $this->relation=$relation;
    }
    
    //魔术方法:查询器    
    public function __get($attribute)
    {
        $msg=null;
        if(isset($this->$attribute)){
            $msg= $this->$attribute;            
        }elseif(isset ($this->data[$attribute])){
            $msg= $this->data[$attribute];            
        }else{
            $msg='无此属性';
        }
        return $msg;
    }
    
    //魔术方法:设置器
    public function __set($attribute,$value)
    {
        if(isset($this->$attribute)){
            $this->$attribute=$value;            
        }else{
            $this->data[$attribute]=$value;//如果属性不存在,创建它并保存到类属性$data数组中
        }
    }
    
    //将父类方法进行重写
    public function where()
    {//这里$this->name不能换成self::name
        return $this->name.'住在'.parent::where();
    }
    
    public function getConst()
    {
        return self::COMPANY;
    }
}

运行实例 »

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


3、测试代码ceshi.php:

实例

<?php

/* 
 * 类的继承与方法重载
 */
//使用自动加载器来加载类:(简写版)
spl_autoload_register(function($className){
    require './class/'.$className.'.php'; 
});

$person1 = new Person1('钱天行',12,['看电视','做作业','吃饭'],'儿子');
//下面我们换一组数据来初始化对象,验证parent::__contrunctor()
echo '姓名: ',$person1->name,'<br>';
echo '年龄: ',$person1->age, '<br>';
echo '<pre>'.'爱好: ', print_r($person1->hobby,true), '<br>'.'</pre>';
echo '关系:',$person1->relation,'<br>';
////获取一个不存在的属性
echo '性别:', $person1->sex, '<br>';        
        
echo '<hr>';
echo $person1->where().'<br>'; //where()是父类中的方法
echo $person1->name.'的父亲工作单位是:'.$person1->getConst();

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


4、运行结果

1.png

批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系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+教程免费学