php面向对象子类继承父类并且传值案例代码

[复制链接]
查看1505 | 回复0 | 2020-11-3 08:48 | 显示全部楼层 |阅读模式

php面向对象子类继承父类并且传值案例代码
  1. <?php
  2. class Person{
  3.     protected $name;
  4.     protected $sex;
  5.     public function __construct($name,$sex){
  6.         $this->name = $name;
  7.         $this->sex = $sex;
  8.     }
  9. }

  10. class Student extends Person{
  11.     private $score;
  12.     public function __construct($name,$sex,$score){
  13.         parent::__construct($name,$sex);
  14.         $this->score=$score;
  15.     }
  16.     public function getInfo(){
  17.         echo "姓名为:".$this->name ."<br />";
  18.         echo "性别为:".$this->sex ."<br />";
  19.         echo "得分为:".$this->score ."<br />";
  20.     }   
  21. }

  22. $stu = new Student('小哥','男',99);
  23. $stu->getInfo();
  24. ?>
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-4-16