ArkTS##HarmonyOS SDK application service##socialize
Core concepts
Method Overloading refers to allowing multiple methods with the same name in the same class, but these methods must have different parameter lists (differences in parameter types, quantities, or orders). Overloading is an implementation of compile time polymorphism (static polymorphism), where the compiler automatically selects matching methods based on the parameters provided at the time of invocation.
Overloading rules
- Same method name: All overloaded methods must have the same name.
- Different parameter lists: At least one of the types, quantities, or orders of the parameters is different.
- The return type is independent: simply having different return types is not enough to constitute an overload (for example, int func() and double func() are not overloaded).
- Access modifiers are irrelevant: method access permissions (such as public/private) do not affect overloading.
The underlying principle of overloading
- Compile time parsing: The compiler selects the most matching method from all methods with the same name based on the parameter type and quantity at the time of method invocation.
- Parameter type elevation: If the parameter type can be implicitly converted (such as int to double), the compiler will attempt to match more general methods.
- Ambiguity error: If the compiler cannot explicitly select a method (for example, both methods may match), a compilation error will be reported.
Practical application scenarios for overloading
- Simplify API design: Provide multiple methods with the same name, allowing users to flexibly call them based on the type and number of input parameters.
- Enhance code readability: For example, print (String) and print (int) are more intuitive than printString and printInt.
- Support default parameters (in some languages): such as C++, which simulates the effect of default parameters through overloading.
precautions
- Avoid excessive overloading: Too many overloading methods may make the code difficult to maintain.
- Ambiguity in parameter types: for example, adding (int, long) and adding (long, int) may cause confusion during invocation.
- Varargs: Varargs in Java, such as void funcs (String... ARGs), can coexist with other overloaded methods, but they need to be carefully designed to avoid ambiguity.
summarize
Method overloading is a powerful language feature that improves code flexibility and readability through compile time polymorphism. Reasonable use of overloading can simplify API design, but it is important to avoid excessive complexity. Understanding the difference between overloading and rewriting, as well as the support for overloading in different languages, is one of the key to mastering object-oriented programming.
Practical scenario: There are two ways to log in. 1. Log in through your phone number. 2. Login through authentication string.
Code Practice:
@Entry
@Component
struct FunctionOverloading {
build() {
Column({space:10}) {
Text('FunctionOverloading')
Button('login by phone').onClick(()=>{
login('13512341234','pwd123')
})
Button('login by authcode').onClick(()=>{
login('authcode123')
})
}
.height('100%')
.width('100%')
}
}
/**
* 手机验证码登录
* @param phone
* @param code
*/
function login(phone: string, code: string);
/**
* 通过authcode登录
* @param authcode
*/
function login(authcode: string);
function login(param1: string, param2?: string) {
if (param2 !== undefined) {
console.info('login','通过手机验证码登录', `phone=${param1}, code=${param2}`)
} else {
console.info('login', '通过token登录', `authcode=${param1}`)
}
}
Click the "login by phone" button to output the log:
05-13 10:30:35.857 7201-7201 A03D00/com.thi....hosapp/JSAPP com.third...g.hosapp I login 通过手机验证码登录 phone=13512341234, code=pwd123
Click the "login by authcode" button to output the log:
05-13 10:30:36.524 7201-7201 A03D00/com.thi....hosapp/JSAPP com.third...g.hosapp I login 通过token登录 authcode=authcode123
Top comments (0)