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

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
