php 父类读取子类const,5月3日作业 —— 父类与子类

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

1、父类代码class/Family.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:

实例

/**

* 创建个人类: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:

实例

/*

* 类的继承与方法重载

*/

//使用自动加载器来加载类:(简写版)

spl_autoload_register(function($className){

require './class/'.$className.'.php';

});

$person1 = new Person1('钱天行',12,['看电视','做作业','吃饭'],'儿子');

//下面我们换一组数据来初始化对象,验证parent::__contrunctor()

echo '姓名: ',$person1->name,'
';

echo '年龄: ',$person1->age, '
';

echo '

'.'爱好: ', print_r($person1->hobby,true), '
'.'
';

echo '关系:',$person1->relation,'
';

获取一个不存在的属性

echo '性别:', $person1->sex, '
';

echo '


';

echo $person1->where().'
'; //where()是父类中的方法

echo $person1->name.'的父亲工作单位是:'.$person1->getConst();

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

4、运行结果

a76282b63ccfa9ce754b05f4e290d65c.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值