找回密码
 免费注册

php开发教程子类中调用父类成员

[复制链接]
发表于 2022-12-6 10:36:47 | 显示全部楼层 |阅读模式
php开发教程子类中调用父类成员

  1. <?php
  2. //父类
  3. class Person {
  4.         public function show() {
  5.                 echo '这是人类<br>';
  6.         }
  7. }
  8. //子类
  9. class Student extends Person {
  10.         public function test() {
  11.                 //方法一;
  12.                 /*
  13.                 $person=new Person();
  14.                 $person->show();                //这是人类
  15.                 */
  16.                 //方法二
  17.                 $this->show();                        //这是人类
  18.         }
  19. }
  20. //测试
  21. $stu=new Student;
  22. $stu->test();
复制代码
小结:
1、方法一:通过实例化父类调用父类的成员
2、方法二:通过$this关键字调用父类的成员

QQ|Archiver|小黑屋|网站地图|深圳市金黑网络技术有限公司 ( 粤ICP备2021124338号|粤公网安备 44030702003689号 )

GMT+8, 2026-8-8 17:09 , Processed in 0.044619 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表