函数的重载(OVERLOAD)

函数的重载:在同一个类中,可以共存多个相同名称的方法,但前提条件是这些方法必须在参数的个数或类型上不同(只是返回值不同不能通过编译器),构造函数同样可以重载。

例子:

 public class Test {
  public int sum(int a,int b) {
   return a + b;
  }
  public int sum(int a,int b,int c) {
   return a + b + c;
  }
  public int sum(int a,int b,int c,int d) {
   return a + b + c + d;
  }
  public int sum(int a,int b,int c,int d,int e) {
   return a + b + c + d + e;
  }
  
  public static void main(String[] args) {
   Test t = new Test();
   int a = 1;
   int b = 2;
   int c = 3;
   int d = 4;
   int e = 5;
   System.out.println("Strat Test");
   System.out.println(sum(a,b));
   System.out.println(sum(a,b,c));
   System.out.println(sum(a,b,c,d));
   System.out.println(sum(a,b,c,d,e));  

 
  }
 }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值