当前位置:网站首页>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
?>
边栏推荐
- Visual Studio導入
- RGB 无限立方体(高级版)
- 在线音乐播放器app
- Ubuntu 20.04 installing mysql8
- A collection of commonly used plug-ins for idea development tools
- 3D printer G code command: complete list and tutorial
- “簡單”的無限魔方
- brew install * 失败,解决方法
- Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
- Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
猜你喜欢

Installation du tutoriel MySQL 8.0.22 par centos8

Go language web development is very simple: use templates to separate views from logic

Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders

2022-2-14 learning xiangniuke project - Section 6 displays login information

青训营--数据库实操项目

Technologists talk about open source: This is not just using love to generate electricity

Vite打包后的dist不能直接在浏览器打开吗

Online music player app

Win10 copy files, save files... All need administrator permission, solution

Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
随机推荐
中小型项目手撸过滤器实现认证与授权
1037 Magic Coupon
Cube magique infini "simple"
Determine whether there is an element in the string type
Software testing learning - day 4
Fabric. JS background is not affected by viewport transformation
centos8安裝mysql8.0.22教程
运动健身的一些心得经验
GRBL 软件:简单解释的基础知识
Basic use of form
Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import
生成二维码
Technologists talk about open source: This is not just using love to generate electricity
MySQL foundation --- query (learn MySQL foundation in 1 day)
Fabric. JS iText sets the color and background color of the specified text
Gee series: Unit 4 data import and export in Google Earth engine
RNN recurrent neural network
Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
Fabric. JS 3 APIs to set canvas width and height
Foreign trade marketing website system development function case making