当前位置:网站首页>php父类(parent)
php父类(parent)
2022-07-02 05:47:00 【杰儿__er】
php parent在面向对象语法中代表“父类”,本质上就是代表父类这个“类”,而不是父类的“对象”,其使用方式为“parent::属性或方法;”。
关键字 | 含义 | 使用位置 | 使用示例 |
---|---|---|---|
parent: | 代表父类(这个类) | 肯定在一个方法中 | parent::属性或方法; |
self: | 代表当前其所在的类 | 肯定在一个方法中 | self::静态属性或方法 |
$this: | 代表调用当前方法的对象 | 肯定在一个方法中 | $this->实例属性或方法; |
<?php
class Person{
protected $name;
protected $sex;
protected $age;
function __construct($name="", $sex="男", $age=1){
$this->name = $name;
$this->sex = $sex;
$this->age = $age;
}
function say(){
echo "我的名字: ".$this->name.", 性别: ".$this->sex,", 年龄: ".$this->age;
}
}
class Student extends Person{
private $school; //在学生类中添加一个所在学校的school的成员属性。
//覆盖父类中的构造方法,在参数列表内加一个学校属性,用来创建对象并初始化成员属性
function __construct($name="", $sex="男", $age=1, $school=""){
parent::__construct($name, $sex, $age);
$this->school = $school;
}
function stydy(){
echo $this-name."正在".$this->school."学习";
}
function say(){
parent::say(); //调用父类中被本方法覆盖掉的方法
echo "在".$this->school."学校上学";
}
}
$student = new Student("张三", "女", "23", "edu");//创建一个学生对象,并传给一个学校名称参数
$student->say();//调用学生类中覆盖父类的说话方法
/**结果是: 我的名字: 张三, 性别: 女, 年龄: 23在edu学校上学 */
//注意:在子类中重写的方法访问权限一定不能低于父类被覆盖的放的的访问权限。
//例如父类中方法的访问权限是protected,那么子类中重写的方法的访问权限就要是protected或者public
?>
边栏推荐
- Financial portal related information
- 2022-2-14 learning xiangniuke project - Section 6 displays login information
- Usage record of vector
- 1037 Magic Coupon
- "Simple" infinite magic cube
- VSCode paste image插件保存图片路径设置
- Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
- Disable access to external entities in XML parsing
- Fabric. JS 3 APIs to set canvas width and height
- all3dp.com网站中全部Arduino项目(2022.7.1)
猜你喜欢
Lantern Festival gift - plant vs zombie game (realized by Matlab)
Fabric. JS iText sets the color and background color of the specified text
软件测试 - 概念篇
Fabric. JS iText superscript and subscript
Opencv LBP features
3D printer G code command: complete list and tutorial
Fabric. JS centered element
MySQL foundation --- query (learn MySQL foundation in 1 day)
Cube magique infini "simple"
“简单”的无限魔方
随机推荐
Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
Appnuim environment configuration and basic knowledge
Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash
460. LFU 缓存 双向链表
Fabric. JS 3 APIs to set canvas width and height
Ls1046nfs mount file system
Fabric. JS upload local image to canvas background
How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
数据挖掘方向研究生常用网站
1036 Boys vs Girls
h5跳小程序
2022-2-14 learning xiangniuke project - Section 6 displays login information
Generate QR code
RGB infinite cube (advanced version)
生成二维码
mysql事务和隔离级别
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
VSCode paste image插件保存图片路径设置
“簡單”的無限魔方
Minimum value ruler method for the length of continuous subsequences whose sum is not less than s