当前位置:网站首页>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
?>
边栏推荐
- JVM class loading mechanism
- 6. Network - Foundation
- Foreign trade marketing website system development function case making
- 15 C language advanced dynamic memory management
- 2022-2-14 learning xiangniuke project - Section 7 account setting
- 文件包含漏洞(一)
- h5跳小程序
- Zzuli: maximum Convention and minimum common multiple
- 文件包含漏洞(二)
- ThreadLocal memory leak
猜你喜欢

7. Eleven state sets of TCP

数理统计与机器学习
![Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]](/img/78/a17034d4b77d5c0dbe741f84a8ecd7.jpg)
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]

Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond

2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions

idea开发工具常用的插件合集汇总

Pytorch Basics

mysql事务和隔离级别

Fabric. JS iText set italics manually

Win10 copy files, save files... All need administrator permission, solution
随机推荐
文件包含漏洞(二)
Grbl software: basic knowledge of simple explanation
Alibaba: open source and self-developed liquid cooling data center technology
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
RGB infinite cube (advanced version)
Installation du tutoriel MySQL 8.0.22 par centos8
Financial portal related information
Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash
Common protocols and download paths of NR
Uva548 tree
Cube magique infini "simple"
Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]
Thread pool overview
Pytorch Chinese document
5g market trend in 2020
Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
XSS basic content learning (continuous update)
h5跳小程序