方法重载

 

//定义一个main方法 
    
    public static void main(String[] args){
        Methods getm=new Methods();
        int res1=getm.m(2,1);
        System.out.print("max1:"+res1+" ");
        double res2=getm.m(2.0,3.0);
        System.out.print("max2:"+res2+" ");
        double res3=getm.m(4.0,5.0,6.0);
        System.out.print("max3:"+res3);
        }

    }
    class Methods{
        public static int m(int a,int b){
            if(a>b){
                return a;
            }else
            return b;

        }//结束第一个m方法
        public static double m(double a,double b){
            if(a>b){
                return a;
            }else
            return b;


        }
        public static double m(double a,double b,double c){
            double [] arr={a,b,c};
            double max=arr[0];
            for(int i=0;i<3;i++){
                if(arr[i]>max){
                    max=arr[i];

                }

            }
            return max;
        }
    }//结束Methods方法

 

posted @ 2025-05-09 16:48  胖豆芽  阅读(7)  评论(0)    收藏  举报