php面向对象子类继承父类并且传值案例代码
- <?php
- class Person{
- protected $name;
- protected $sex;
- public function __construct($name,$sex){
- $this->name = $name;
- $this->sex = $sex;
- }
- }
- class Student extends Person{
- private $score;
- public function __construct($name,$sex,$score){
- parent::__construct($name,$sex);
- $this->score=$score;
- }
- public function getInfo(){
- echo "姓名为:".$this->name ."<br />";
- echo "性别为:".$this->sex ."<br />";
- echo "得分为:".$this->score ."<br />";
- }
- }
- $stu = new Student('小哥','男',99);
- $stu->getInfo();
- ?>
复制代码
|
|