// 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;
}
}
在JAVA中,函数名字相同,返回类型不同是允许的,在调用过程中,调用int还是double取决于函数square()括号里的参数是int还是double,在调用相应函数。
原文:http://www.cnblogs.com/shouhutian/p/5966151.html