Java basic study notes _ method overloading

1. What is method overloading?

Multiple methods in the same class have the same method name but different parameter lists. This situation is called method overloading .

Method overloading has nothing to do with the type of return value.

The parameter list is different:

1. The number of parameters is different

2. The parameter type of the corresponding position is different

Method signature: method name + parameter list

2. Why do I need method overloading?

When the implemented functions are the same, but the specific implementation methods are different, we can better manage the methods in the class by defining methods with the same name and different parameters (conditions).

public static int sum(int a, int b) {
    return a + b;
}

public static long sum(long a, long b) {
    return a + b;
}

public static double sum(double a, float b, int c) {
    return a + b + c;
}

 

Guess you like

Origin blog.csdn.net/qq_43191910/article/details/114749817
Recommended