Home php教程 php手册 php面向对象全攻略 (八)重载新的方法_php基础

php面向对象全攻略 (八)重载新的方法_php基础

May 17, 2016 am 09:02 AM
php Overload

12.重载新的方法
在学习PHP 这种语言中你会发现,PHP 中的方法是不能重载的,所谓的方法重载就是
定义相同的方法名,通过“参数的个数”不同或“参数的类型”不同,来访问我们的相同方法
名的不同方法。但是因为PHP 是弱类型的语言,所以在方法的参数中本身就可以接收不同类
型的数据,又因为PHP 的方法可以接收不定个数的参数,所以通过传递不同个数的参数调用
不相同方法名的不同方法也是不成立的。所以在PHP 里面没有方法重载。不能重载也就是在
你的项目中不能定义相同方法名的方法。另外,因为PHP 没有名子空间的概念,在同一个页
面和被包含的页面中不能定义相同名称的方法,也不能定义和PHP 给我提供的方法重名,当
然在同一个类中也不能定义相同名称的方法。
我们这里所指的重载新的方法所指的是什么呢?其实我们所说的重载新的方法就是子类
覆盖父类的已有的方法,那为什么要这么做呢?父类的方法不是可以继承过来直接用吗?但
有一些情况是我们必须要覆盖的,比如说我们前面提到过的例子里面,“Person”这个人类里
面有一个“说话”的方法,所有继承“Person”类的子类都是可以“说话”的,我们“Student”
类就是“Person”类的子类,所以“Student”的实例就可以“说话”了,但是人类里面“说
话”的方法里面说出的是“Person”类里面的属性,而“Student”类对“Person”类进行了扩
展,又扩展出了几个新的属性,如果使用继承过来的“say()”说话方法的话,只能说出从
“Person”类继承过来的那些属性,那么新扩展的那些属性使用这个继承过来的“say()”的
方法就说不出来了,那有的人就问了,我在“Student”这个子类中再定义一个新的方法用于
说话,说出子类里面所有的属性不就行了吗?一定不要这么做,从抽象的角度来讲,一个“学
生”不能有两种“说话”的方法,就算你定义了两个不同的说话的方法,可以实现你想要的
功能,被继承过来的那个“说话“方法可能没有机会用到了,而且是继承过来的你也删不掉。
这个时候我们就要用到覆盖了。
虽然说在PHP 里面不能定义同名的方法,但是在父子关系的两个类中,我们可以在子类
中定义和父类同名的方法,这样就把父类中继承过来的方法覆盖掉了。
代码片段
复制代码 代码如下:


//定义一个“人”类做为父类
class Person{
//下面是人的成员属性
var $name; //人的名子
var $sex; //人的性别
var $age; //人的年龄
//定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值
function __construct($name, $sex, $age){
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}
//这个人可以说话的方法, 说出自己的属性
function say() {
echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."
";
}
}
class Student extends Person
{
var $school; //学生所在学校的属性
//这个学生学习的方法
function study() {
echo "我的名子叫:".$this->name." 我正在”.$this->school.”学习
";
}
//这个学性可以说话的方法, 说出自己所有的属性,覆盖了父类的同名方法
function say() {
echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."我在
".$this->school."上学.
";
}
}
?>

上面的例子,我们就在“Student”子类里覆盖了继承父类里面的“say()”的方法,通过
覆盖我们就实现了对“方法”扩展。
但是,像这样做虽然解决了我们上面说的问题,但是在实际开发中,一个方法不可能就
一条代码或是几条代码,比如说“Person”类里面的“say()”方法有里面有100 条代码,如
果我们想对这个方法覆盖保留原有的功能外加上一点点功能,就要把原有的100 条代码重写
一次,再加上扩展的几条代码,这还算是好的,而有的情况,父类中的方法是看不见原代码
的,这个时候你怎么去重写原有的代码呢?我们也有解决的办法,就是在子类这个方法中可
以调用到父类中被覆盖的方法,也就是把被覆盖的方法原有的功能拿过来再加上自己的一点
功能,可以通过两种方法实现在子类的方法中调用父类被覆盖的方法:
一种是使用父类的“类名::“来调用父类中被覆盖的方法;
一种是使用“parent::”的方试来调用父类中被覆盖的方法;
代码片段
复制代码 代码如下:

class Student extends Person{
var $school; //学生所在学校的属性
//这个学生学习的方法
function study() {
echo "我的名子叫:".$this->name." 我正在”.$this->school.”学习
";
}
//这个学性可以说话的方法, 说出自己所有的属性,覆盖了父类的同名方法
function say() {
//使用父类的“类名::“来调用父类中被覆盖的方法;
// Person::say();
//或者使用“parent::”的方试来调用父类中被覆盖的方法;
parent::say();
//加上一点自己的功能
echo “我的年龄是:".$this->age."我在".$this->school."上学.
";
}
}

现在用两种方式都可以访问到父类中被覆盖的方法,我们选那种方式最好呢?用户可能
会发现自己写的代码访问了父类的变量和函数。如果子类非常精炼或者父类非常专业化的时
候尤其是这样。不要用代码中父类文字上的名字,应该用特殊的名字parent,它指的就是子
类在extends 声明中所指的父类的名字。这样做可以避免在多个地方使用父类的名字。如果继
承树在实现的过程中要修改,只要简单地修改类中extends 声明的部分。
同样,构造方法在子类中如果没有声明的话,也可以使用父类中的构造方法,如果子类
中重新定义了一个构造方法也会覆盖掉父类中的构造方法,如果想使用新的构造方法为所有
属性赋值也可以用同样的方式。
代码片段
复制代码 代码如下:

class Student extends Person{
var $school; //学生所在学校的属性
function __construct($name, $sex, $age, $school){
//使用父类中的方法为原有的属性赋值
parent::__construct($name, $sex, $age);
$this->school=$school;
}
//这个学生学习的方法
function study() {
echo "我的名子叫:".$this->name." 我正在”.$this->school.”学习
";
}
//这个人可以说话的方法, 说出自己的属性
function say() {
parent::say();
//加上一点自己的功能
echo “我的年龄是:".$this->age."我在".$this->school."上学.
";
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles