Java third week of after-school title

// MethodOverload.java
// Using overloaded methods

public class MethodOverload {

    public static void main(String[] args) {
        System.out.println("The square of integer 7 is " + square(7));
        System.out.println("\nThe square of double 7.5 is " + square(7.5));
    }

    public static int square(int x) {
        return x * x;
    }

    public static double square(double y) {
        return y * y;
    }
}
Copy the code

Code shows the above-described exemplary Java "Method overloads (overload)" characteristic.

Two or more of the following methods constituting condition "Overload" Relationship: same as (1) a method name; product (2) different types of parameters, number of different parameters, the parameters or the order of different type.

Note: Returns the value of the determination condition as a method of method overloading.

// MethodOverload.java
// Using overloaded methods

public class MethodOverload {

    public static void main(String[] args) {
        System.out.println("The square of integer 7 is " + square(7));
        System.out.println("\nThe square of double 7.5 is " + square(7.5));
    }

    public static int square(int x) {
        return x * x;
    }

    public static double square(double y) {
        return y * y;
    }
}
Copy the code

Code shows the above-described exemplary Java "Method overloads (overload)" characteristic.

Two or more of the following methods constituting condition "Overload" Relationship: same as (1) a method name; product (2) different types of parameters, number of different parameters, the parameters or the order of different type.

Note: Returns the value of the determination condition as a method of method overloading.

Guess you like

Origin www.cnblogs.com/aiyyue/p/11599943.html
Recommended