Home > Backend Development > PHP Tutorial > The difference between method overloading and rewriting, the difference between method overloading and rewriting_PHP tutorial

The difference between method overloading and rewriting, the difference between method overloading and rewriting_PHP tutorial

WBOY
Release: 2016-07-12 08:53:56
Original
2022 people have browsed it

The difference between method overloading and rewriting, the difference between method overloading and rewriting

Answer 1: Fundamental difference:
Rewriting (refactoring) is very intuitive. The subclass covers the method of the parent class with the same name, parameters and return value. Only the rewritten method is called by the subclass
Overloaded, just with the same name.
​ ​ Answer 2: 1. Overloading:
The method name is the same, but the parameter list is different
2. Rewriting:
is also called overwriting, which means defining a method in the subclass with the same name and parameter list as the method in the parent class method. Because the subclass will inherit the methods of the parent class, and rewriting means redefining the methods inherited from the parent class and refilling the code in the method.
​ ​
Answer three: ​
重写是子类的方法覆盖父类的方法,要求方法名和参数都相同<br /><br />重载是在同一个类中的两个或两个以上的方法,拥有相同的方法名,但是参数却不相同,方法体也不相同,最常见的重载的例子就是类的构造函数,可以参考API帮助文档看看类的构造方法<br /><br /><br /><br />
Copy after login
Answer four:
1. Overriding must be inherited, overloading is not used.
2. The overridden method names have the same number of parameters and compatible parameter types. The overloaded method names have the same name but different parameter lists.
3. The overridden method modifier is greater than or equal to the parent class method, and the overloading has nothing to do with the modifier.
4. Rewriting cannot throw general exceptions that are not thrown by the parent class, but can throw runtime exceptions

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1122389.htmlTechArticleThe difference between method overloading and rewriting, the difference between method overloading and rewriting Answer 1: Fundamental difference: Rewriting ( Refactoring) is very intuitive. The subclass covers the method of the parent class with the same name, parameters and return value. Use the subclass...
Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template