Home > php教程 > php手册 > 探讨方法的重写(覆载)详解

探讨方法的重写(覆载)详解

WBOY
Release: 2016-06-13 11:50:02
Original
878 people have browsed it

复制代码 代码如下:


 class Cart{
  public function Cart(){
   echo "正在调用Cart()
";
  }
  public function doSomething(){
   echo "正在调用doSomethimg()
";
  }
 }
 class Named_Cart extends Cart{
  function Named_Cart(){
   echo "正在调用Named_Cart()
";
  }
  function doSomething(){
   echo "正在调用Named_Cart::doSomething()
";
  }
 }
$myCart=new Cart();
$myCart->doSomething();
$myNamed_Cart=new Named_Cart();
$myNamed_Cart->doSomething();
?>


在方法覆盖时,务必要使用与原始方法相同的约定,包括参数要一致。属性覆盖也遵循相同的约定。
在覆盖基类的方法后,使用parent关键字仍然可以调用基类的doSomething()方法,而非当前类中的doSomething()方法。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template