[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
【1】什么是方法的重载:
方法的重载是在一个类中可以定义多个方法名相同,但参数不同的方法。调用时,会根据不同参数自动匹配对应的方法。
本质:重载的方法,实际是完全不同的方法,仅仅是名称相同。
【2】构成方法重载的条件
(1)不同的含义:形参类型,形参个数,形参的顺序不同
(2)只有返回值不同不构成方法的重载
eg:int a(String str){}与 void a (String str){}不构成方法的重载
(3)只有形参的名称不同,不构成方法的重载
eg:int a(String str){}与int a(String s){}不构成方法的重载
【3】代码的演示
public class TestMethod06{ public static void main(String[] args){ //10+20 int
sum = add(10,20); System.out.println(sum); //10+20+30
System.out.println(add(10,20,30)); //20+40+60+80
System.out.println(add(20,40,60,80)); //9.9+0.1
System.out.println(add(9.9,0.1)); } //定义一个方法,两个数相加: public static int add(int
num1,int num2){ return num1+num2; } //定义一个方法,三个数相加 public static int add(int
num1,int num2,int num3){ return num1+num2+num3; } //定义一个方法,四个数相加 public static
int add(int num1,int num2,int num3,int num4){ return num1+num2+num3+num4; }
//定义一个方法:两个数相加,两个double类型的数相加 public static double add(double num1,double
num2){ return num1+num2; } } //同一个类中可以存在方法名相同的多个方法 //方法名相同,形参列表不同,这些方法就构成了方法的重载
//方法名(eg:add) 形参列表(eg:int num1,double num2)
总结:
1.方法的重载:在同一个类中,方法名相同,形参列表不同的多个方法,构成了方法的重载
2.方法的重载只跟方法名和形参列表有关,与修饰符,返回值类型无关
3.注意:形参列表不同指的是什么?
(1)个数不同
add() add(int num1) add(int num1,int num2)
(2)顺序不同
add(int num1,double num2) add(double num1,int num2)
(3)类型不同
add(int num1) add(double num1)
4.请问下面的方法是否构成方法的重载?
(1)add(int a) 和 add(int b) ---->不构成,相当于方法的重复定义
(2)public static int add(int a) 和 public static void add(int a) ----->不构成