函数的重载,

来源:互联网 发布:局部搜索算法历史 编辑:程序博客网 时间:2024/06/05 16:22

函数的重载:

函数重载的意义就是:为了方便表示统一功能的函数

什么时候函数可以重载:

只是参数不同时:  比如参数的类型,参数的数量,参数的顺序;

就可以使用函数的重载

// 写比较俩个数的大小;

// 比较3个数的大小

//比较4个数的大小

// B比较5个数的大小;

public class sort{

public static void main(String[] args){


  sort(0,0);

        System.out.println(sort(1,2,3,1)); /// 要比较几个数的值的大小就输入几个数;  // 这里是调用函数;

}

//写函数

   public static int sort(int a, int b){

return ( a > b ) ? a: b;

}

 public static int sort(int a, int b,int c){

int max=sort(a,b)

return sort(max,c);

 public static int sort(int a, int b,int c, int d ){

int max=sort(max,c)

return sort(max,d);

 public static int sort(int a, int b,int c, int d int e ){

int max=sort(max,d)

return sort(max,e);


}





}

原创粉丝点击