分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

PHP类(四)-类的继承

发布时间:2023-09-06 01:33责任编辑:蔡小小关键词:PHP

类的继承就是从已经定义的类中继承数据,也可以重新定义或者加入一些数据。

被继承的类称为父类,基类,超类

继承的类称为子类,派生类

在PHP中只能使用单继承,也就是一个类只能从一个类中继承数据,但是一个类可以有多个子类

<?phpclass Person{var $name;var $age;var $sex;function __construct($name="Alex",$age=12,$sex="Male"){$this->name = $name;$this->age = $age;$this->sex = $sex;}function Say(){echo "My name is ".$this->name.",and my age is ".$this->age.",sex is ".$this->sex;echo "<br>";}}class Student extends Person{var $grade;function Study(){echo $this->name." is study in grade ".$this->grade.".And My age is ".$this->age;echo "<br>";}}class Teacher extends Person{var $subject;function Teach(){echo $this->name." teaches ".$this->subject;echo "<br>";}}$p1 = new Student("John",16,"Male");$p1->Say();$p1->grade = 8;$p1->Study();$p2 = new Teacher("Tom",23,"Male");$p2->Say();$p2->subject = "PHP";$p2->Teach();?>

 运行结果

PHP类(四)-类的继承

原文地址:https://www.cnblogs.com/sch01ar/p/8150409.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved