探讨方法的重写(覆载)详解
复制代码 代码如下:
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()方法。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools
