PHP5中使用parent::来引用父类的方法。

  • parent:: 可用于调用父类中定义的成员方法。
  • parent::的追溯不仅于直接父类。

通过parent::调用父类方法

<!-- 声明一个员工类,经理类继承自员工类 -->
<?
class employee{
    protected  $sal=3000;        
    public function getSal(){
        $this->sal = $this->sal + 1200;        
        return $this->sal ;
    }    
}

class Manager extends employee {
    //如果想让经理在员工工资的基础上多发1500元.
    //必须先调用父类的getSal()方法.
    public function getSal(){        
        parent::getSal();// 这里调用了父类的方法.
        $this->sal = $this->sal + 1500;        
        return $this->sal ;
    }    
}
$emp = new employee();
echo "普通员工的工资是 " . $emp->getSal();
echo "<br>";

$manager = new Manager();
echo "经理的工资是: " . $manager->getSal();
?>
View Code

相关文章:

  • 2021-07-14
  • 2021-05-31
  • 2021-09-02
  • 2021-10-18
  • 2022-01-05
  • 2021-11-18
  • 2021-08-28
  • 2022-12-23
猜你喜欢
  • 2022-03-05
  • 2022-12-23
  • 2021-09-21
  • 2021-10-13
  • 2022-01-03
  • 2022-01-05
  • 2021-04-04
相关资源
相似解决方案