Fork me on GitHub

动手动脑2

package 动手动脑;
//方法超出负载
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 integer 7.5 is"+square(7.5));
     }

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

上面几行代码的特殊之处就是在函数名字相同的情况下,主函数可以正常引用正确的函数,之所以会这样,是因为函数类型不同,在调用函数之前,函数会自动匹配和已有函数类型相同的去引用它。double是双精度浮点型,而计算出来的结果小数点后的位数不对,只能说明是溢出了,超出负载。

posted @ 2018-10-14 16:57  今天123  阅读(77)  评论(0编辑  收藏  举报
1