Home > php教程 > php手册 > body text

php中重写理解

WBOY
Release: 2016-06-21 08:47:28
Original
1033 people have browsed it

我们经常听到面向对象的三大特性:封装、继承、多态,但是还有许多特性,那么我们是否记得重写呢?在学习的时候简单的记录了下php中等重写方法:

1)先通过例子看下,这样更加明了


//定义父类(也可以称为基类)
class Goods {
public $goods_name = 'Goods:name';

public function sayName() {
echo $this->goods_name;
}

}
//定义子类(也可以成为扩展类)
class Book extends Goods {
public $goods_name='Book:name';
public function sayName() {
echo 'run in book';
}
}
//实例化对象
$b1 = new Book;

//输出run in book,因为子类重写了父类的sayName()方法
$b1->sayName();

下章总结下php中的重写调用...



Related labels:
php
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