重载跟继承是相结合的 继承请看 http://0x007.blog.51cto.com/6330498/1101578
重载:
子类当中的方法与父类当中的方法相同,则子类的方法就重写了父类的方法,既覆盖了父类当中的方法。
子类的方法与父类的方法同名。
例:
- <?php
- class ren {
- var $name,$sex,$age;
- function __construct($name,$sex,$age){
- $this->name=$name;
- $this->sex=$sex;
- $this->age=$age;
- }
- function say(){
- echo '我叫'.$this->name.'我的年龄是'.$this->sex.'我的年龄是'.$this->age.'<br/>';
- }
- }
- class student extends ren {
- var $school;
- function __construct($name,$sex,$age,$school){
- $this->name=$name;
- $this->sex=$sex;
- $this->age=$age;
- $this->school=$school;
- }
- function say(){
- echo "看到这句话说明子类的方法覆盖了父类的方法,这叫做重载".'<br/>';
- echo "我的名字叫".$this->name.'我的年龄是'.$this->age;
- }
- function study(){
- echo "我叫".$this->name.'我在'.$this->school.'学习';
- }
- }
- $p = new student ('0x007','男',19,'北京');
- //调用say方法(没有重载之前调用的是父类的,现在student已经重载asy方法 调用的就是student里面的say方法)
- $p -> say();
- //调用study方法
- $p -> study();
- ?>
转载于:https://blog.51cto.com/0x007/1102109