PHP学习笔记5-类的继承/方法重写,学习笔记5-
更改Man.php文件名为People.php,加入代码:
<span>1</span> <span>public</span> <span>function</span><span> hi(){ </span><span>2</span> <span>return</span> <span>$this</span>->_name.' say hi'<span>; </span><span>3</span> }
新建文件Man.php:
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span><span>* </span><span> 3</span> <span> * Created by PhpStorm. </span><span> 4</span> <span> * User: Administrator </span><span> 5</span> <span> * Date: 2015/6/29 </span><span> 6</span> <span> * Time: 11:18 </span><span> 7</span> <span>*/</span> <span> 8</span> <span> 9</span> <span>require_once</span> 'People.php'<span>; </span><span>10</span> <span>11</span> <span>class</span> Man <span>extends</span> People{<span>//</span><span>extends:继承People类</span> <span>12</span> <span>public</span> <span>function</span> __construct(<span>$age</span>,<span>$name</span><span>){ </span><span>13</span> parent::__construct(<span>$age</span>,<span>$name</span>,'男'<span>); </span><span>14</span> <span> } </span><span>15</span> <span>16</span> <span>public</span> <span>function</span> hi(){<span>//</span><span>重写People类的hi方法 </span><span>17</span> <span>// echo parent::hi();//保留父类hi方法</span> <span>18</span> <span>echo</span> 'Man '.<span>$this</span>->getName().' say hi'<span>; </span><span>19</span> <span> } </span><span>20</span> }
如果保留父类方法,可以这么写parent::hi();这里不保留,添加下面一行
修改index.php文件:
<span>1</span> <span>require_once</span> 'Man.php'<span>; </span><span>2</span> <span>$m</span> = <span>new</span> Man(10,'Vito'<span>); </span><span>3</span> <span>echo</span> <span>$m</span>->hi();
最后在网页上可以看到成功输出的信息:Man Vito say hi
Statement
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

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
How to fix KB5055523 fails to install in Windows 11?
3 weeks agoByDDD
How to fix KB5055518 fails to install in Windows 10?
3 weeks agoByDDD
Roblox: Grow A Garden - Complete Mutation Guide
2 weeks agoByDDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
How to fix KB5055612 fails to install in Windows 10?
3 weeks agoByDDD

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
