重载的特点
在同一个类内,有相同的方法和名字,但参数的个数和类型不同,和方法的返回值无关.
比如:
//此类是重载的写法
public class calc{
public int sum(int a,int b){
return a+b;
}
public int sum(float a ,int b){
return a+b;
}
}
//使用
calc c=new calc();
int result1=c.sum(1,2);
float result2=c.sum(1.2F,2);
//此类不是重载的写法
public class calc{
public int sum1(int a,int b){
return a+b;
}
public float sum2(float a,int b){
return a+b;
}
}
//使用
calc c=new calc();
int result1=c.sum1(1,2);
float result2=c.sun2(1.2F,3);
从在的应用场景
功能相似,但功能有一些细微的差异