Home Backend Development PHP Tutorial 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

Jul 29, 2016 am 08:52 AM
Method overloading

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>
Copy after login

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 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 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)

Why does Go language not support the design concept of method overloading? Why does Go language not support the design concept of method overloading? Apr 04, 2024 am 09:00 AM

The Go language does not support method overloading because its design philosophy emphasizes simplicity, concurrency, and type safety. Method overloading can introduce name conflicts, complex type systems, and code confusion. To compensate for this, the Go language provides functions that allow the creation of functions with the same name but different parameter types in the same package, similar to the functionality of method overloading.

Reasons and solutions why method overloading in Go language is not feasible Reasons and solutions why method overloading in Go language is not feasible Apr 03, 2024 pm 12:33 PM

The Go language does not support method overloading due to static type checking complexity, loss of clarity, and incompatibility with interfaces. Alternatives include function overloading, interface methods, and polymorphism. Specifically, function overloading allows the creation of functions of the same name with different parameter lists, interface methods use interfaces to define methods and implement them in different types, and polymorphism uses type conversions and assertions to implement object methods with different types of parameters. transfer.

An in-depth discussion of method overloading issues in Go language An in-depth discussion of method overloading issues in Go language Apr 03, 2024 pm 01:36 PM

The Go language does not support direct method overloading, but uses interfaces to simulate similar functions. The interface defines a set of methods, and the type simulates overloading by implementing the interface's methods. It uses different interfaces to define the same method with different parameter lists, and creates types to implement these interfaces, thereby achieving the effect of method overloading.

How to implement method overloading in Go language How to implement method overloading in Go language Apr 03, 2024 pm 12:15 PM

Method overloading is not supported in Go language, but interface simulation can be used. Method overloading steps: 1. Create an interface containing all possible signatures; 2. Implement multiple methods with different signatures to implement the interface.

Method overloading analysis of Golang functions Method overloading analysis of Golang functions May 16, 2023 am 08:36 AM

In Golang, function overloading (Overloading) is not supported because function names are unique, and defining two functions with the same name in the same scope is not allowed. However, Golang provides an alternative to method overloading, which is method overloading. Method Overloading is a method that defines methods with the same name in a class, but their parameter lists are different. In this article, we will learn about method overloading in Golang in detail. What

How to determine the most matching method in Java function overloading mechanism? How to determine the most matching method in Java function overloading mechanism? Apr 26, 2024 am 09:06 AM

The matching rules for Java function overloading are: Exact match: Parameter type and number exactly match Variable parameters: Variable parameter method matches any number or type of parameters Packaging type and original type conversion: Basic types and packaging types can be converted into each other Automatically loaded Boxing/unboxing: base type values ​​and wrapped type objects can be automatically converted to derived class types: derived class objects can match base class type parameters

An alternative to elegantly handling method overloading in Go An alternative to elegantly handling method overloading in Go Apr 03, 2024 am 10:15 AM

There is no method overloading in Go language, but similar behavior can be achieved using alternatives: Function variables: Define functions with different sets of parameters and store them in variables, calling the appropriate function as needed. Interface type: Define an interface type that contains multiple methods with different sets of parameters and implement the interface to provide specific behavior. Nested types: Grouping methods into nested types, where each nested type represents a function with a different number or type of arguments.

Explore the principles of methods with the same name in Golang Explore the principles of methods with the same name in Golang Feb 23, 2024 pm 10:51 PM

Golang is an open source compiled programming language developed by Google to improve programmer productivity. Methods are an important concept in Golang that allow functions to be defined on specific types. These functions are called methods. In Golang, methods can be defined on structures (structs), interfaces (interfaces) and specific types. When defining methods in a structure or interface, you can use methods with the same name, that is, in the same type, you can define methods with the same name but

See all articles