重载其实很简单,举例如下
//重载
class overload
{
static int show(int a,int b)
{
return (a+b);
}
//err
// int show(int a,int b)
// {
// return (a+b);
// }
// static void show(int a,int b)
// {
// System.out.printf("空");
// }
// static int show(int b,int a)
// {
// return (a+b);
// }
// static double show(int a,int b)
// {
// double t;
// t = a+b;
// return (t);
// }
static double show(double a,double b)
{
return (a+b);
}
static int show(int a,int b,int c)
{
return (a+b+c);
}
public static void main(String[] args)
{
System.out.printf("%d\n",show(1,2));
// show();
System.out.printf("%.2f\n",show(1.0,2.5));
System.out.printf("%d\n",show(1,2,3));
}
}
重载要注意几点:
1、重载指的参数列表不一样(必须),返回值可以一样或者不一样;
2、重载的名字是一样的;
3、main是static,调用的方法也必须是static的,非static调用也必须是非static的