1.在同一个类中,多个方法名相同的存在,但要求形参列表不一致
如:system.out.println();
out 是prinstream类型
2.重载的好处
①减轻了记名的麻烦
②减轻了取名的麻烦
3.注意事项和使用细节
1.方法名:必须相同
2.形参列表:必须不同(形参类型或个数或顺序,至少有一样不同,参数名无要求)
3.返回类型:无要求
4.列题
public class overLoad {
public static void main(String[] args) {
Metool metool=new Metool();
metool.m(10);
metool.m("hallow");
metool.m(10, 30);
}
}
class Metool{
public void m(int n){
System.out.println("m的平方="+(n*n));
}
public void m(int n1,int n2){
System.out.println("m的积="+(n1*n2));
}
public void m(String str){
System.out.println("传入的值是:"+str);
}
}
写三个方法,第一个两个int 类型比较 第二个方法两个double 方法比较 第三个方法 三个double 方法作比较 方法名相同 使用重载
int aint =metool.max(10, 30);
System.out.println(aint);
System.out.println(metool.max(1.1, 23.1));
System.out.println(metool.max(31.5, 15.1, 1));
}
}
class Metool{
public int max(int n1,int n2){
return n1>n2 ? n1:n2;
}
public double max(double n1,double n2){
return n1>n2 ? n1:n2;
}
public double max(double n1,double n2,double n3){
double max1= n1>n2 ? n1:n2;
return max1>n3 ? max1:n3;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有