php 继承_PHP教程
程序员文章站
2022-06-10 12:43:52
...
php 继承
name=$name; $this->age=$age; $this->sex=$sex; $this->school = $school; } function study(){ echo $this->age; echo $this->name.":我在{$this->school}学习
"; } /* 覆盖父类的say(),子类中重载的方法,不能低于父类中的访问权限(子类可以放大权限,但不能缩小权限) 父类中是public function say(){} 子类中变成了private function say(){} private function say(){ echo "{$this->name}:我在{$this->school}学习呢,请不要打扰我
"; } 这时候会报错Fatal error: Access level to Student::say() must be public (as in class Person */ public function say(){ parent::say(); echo "{$this->name}:我在{$this->school}学习呢,请不要打扰我
"; } } #$student = new Student; #$student->school="北京大学附属中学"; #$student->name="haha"; #$student->age=32; #$student->study(); #$student->say(); $student2 = new Student("李会东",24,"男","北京大学"); #echo $student2->school; $student2->say(); ?>
上一篇: 百度api根据文字地址换取坐标点