Home  >  Article  >  Backend Development  >  The difference between method overloading and overriding Benefits of method overloading Overloading Method overloading Return value

The difference between method overloading and overriding Benefits of method overloading Overloading Method overloading Return value

WBOY
WBOYOriginal
2016-07-29 08:52:442910browse

Answer 1:

The fundamental difference:
Rewriting (refactoring) is very intuitive. The subclass overrides 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 the same name.

Answer 2:

1. Overloading:
The method name is the same but the parameter list is different
2. Rewriting:
Also called overwriting, it means defining a method in the subclass with the same name as the method in the parent class Parameter list 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 3:

重写是子类的方法覆盖父类的方法,要求方法名和参数都相同<br>重载是在同一个类中的两个或两个以上的方法,拥有相同的方法名,但是参数却不相同,方法体也不相同,最常见的重载的例子就是类的构造函数,可以参考API帮助文档看看类的构造方法<br>

Answer 4:

1. Overriding must be inherited, overloading is not required.
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

The above has introduced the difference between method overloading and rewriting, including the content of method overloading. I hope it will be helpful to friends who are interested in PHP tutorials.

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