php面向对象中子类中重载父类详解
本文章来给大家整理一下关于php面向对象中子类中重载父类详解,希望此文章对各位理解php子类中重载父类有所帮助哦。
因为在PHP中不能存在同名的函数,所以在同一个类中也就不能定义重名的方法。这里所说的重载是指在 子类中可以定义和父类同名的方法从而覆盖从父类中继承过来的方法。
子类中重载父类的方法
代码如下 | 复制代码 |
|
重写方法与访问权限
子类中的覆盖方法不能使用比父类中被覆盖方法更严格的访问权限。
如果父类中的方法的访问权限是protected,那么子类中重写的方法的权限就要是protected或者public; 如果父类中的方法是public,那么子类要重写的方法的权限就只能是public。也许这也就是为什么子类可 以继承父类的私有成员,但却不能使用的原因吧。
重写时的参数数量
子类可以拥有与父类不同的参数数量,如下面的构造方法中,多添加了一个参数$age。
代码如下 | 复制代码 |
class Student extends Person{
public $name; public function __construct($name="",$age=25){ $this->age =$age; } public function say(){ echo "我叫".$this->name .",今年".$this->age."岁了" ; } } |
构造函数重写
上面提到的“重写时的参数数量”就已经实现了子类对父类的构造函数进行了重写,但这不是一种好的写 法,如果仔细观察,你会发现,上面子类Student对父类Person构造函数的重写,其实就是在父类的构造 函数的基础上多添加了一个参数,但是又把父类原有的参数照写一遍,因为父类Person的构造函数只有一 个参数,所以我们照写一遍不觉得有什么麻烦,但是如果参数不止一个,而是几个或者更多,那么你就会 发现它的繁琐之处,那么有没有办法可以简化这个问题呢?答案是肯定的,可通过使用"parent::方法名" 在子类的重载方法中调用父类中被它覆盖的方法。如使用"parent::__construct()"调用父类中被覆盖的 构造方法,其它方法的类似,于是上面的代码可以简化为:
代码如下 | 复制代码 |
class Student extends Person{
public $name; public $age; parent::__construct($name,$age); $this->age =$age; public function say(){ echo ",今年".$this->age."岁了" ; } |
下再看一个实例
PHP5重写方法
先设置一个父类,这个父类是 “Dog”类,这个类描述了dog的特性。
Dog有2个眼睛,会跑,会叫。就这样描述先。
我养了一直狗,是只小狗,符合Dog类的特性,但有所不同。
我的小狗有名字,我的小狗太小了,不会大声的叫,只会哼哼。
我们用继承的概念去实现这个设计。
代码如下 | 复制代码 |
程序运行结果: dog have 2 eyes. 狗狗 have 2 eyes. |
重写方法与访问权限
子类中的覆盖方法不能使用比父类中被覆盖方法更严格的访问权限。
父类为public 子类为 private时。
代码如下 | 复制代码 |
class MyDog extends Dog { ?> 程序运行结果: |
父类为public 子类为 protected时。
代码如下 | 复制代码 |
class MyDog extends Dog { ?> 程序运行结果: Fatal error: Access level to MyDog::getEyeNumber() must be public (as in class Dog) in E:PHPProjectstest.php on line 15 |
重写时的参数数量
子类可以拥有与父类不同的参数数量。(这点与java不同,PHP是弱类型语言。)
代码如下 | 复制代码 |
$myDog = new MyDog(); 程序运行结果: my dog hava 3 eyes. |
构造函数重写
下面这个例子中,父类和子类都有自己的构造函数,当子类被实例化时,子类的构造函数被调用,而父类的构造函数没有被调用,请对比第一节的构造函数继承。
代码如下 | 复制代码 |
程序运行结果: I am a Dog . |
注:这点和Java不同,在java中构造函数是不能被继承的,而且子类实例化时,子类的构造函数被调用,父类的构造函数也会调用。

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.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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

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