方法的重载

方法的重载在于通过不同的形参调用不同的方法(形参的个数,类型等不同),这样可以随时调用自己想要的方法

public class TestOverload {
public static void main(String[] args) {

System.out.println(add(40, 50));
System.out.println(add(10, 20, 30));
}

public static int add(int n1, int n2) {
int sum = n1 + n2;
return sum;
}

public static int add(int n1, int n2, int n3) {
int sum = n1 + n2 + n3;
return sum;
}

posted @ 2022-03-11 11:53  许我半盏清茶  阅读(27)  评论(0)    收藏  举报