方法的重载及参数传递+方法的操作数组的练习

方法重载的概述和基本使用

在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。

方法重载特点

与返回值类型无关,只看方法名和参数列表

在调用时,虚拟机通过参数列表的不同来区分同名方法



package com.itheima_03;

 

/*

 * 方法重载:在同一个类中,出现了方法名相同的方法,这就是方法重载。

 * 方法重载特点:

 *      方法名相同,参数列表不同。与返回值无关。

 *      参数列表不同:

 *          参数的个数不同。

 *          参数对应的类型不同。

 * 注意:

 *      在调用方法的时候,java虚拟机会通过参数列表的不同来区分同名的方法。

 */

public class MethodDemo {

    public static
void main(String[] args) {

        int a = 10;

        int b = 20;

 

        // 求和

        int result
= sum(a, b);

        System.out.println("result:"
+ result);

 

        int c = 30;

        // 求和

        //int result2 = sum2(a,b,c);

        //System.out.println("result2:"+result2);

        result = sum(a,b,c);

        System.out.println("result:"+result);

    }

    

    

    //两个float类型的数据求和

    public static
float sum(float
a,float
b) {

        return a + b;

    }

 

    // 三个整数的求和

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

        return a + b + c;

    }

    

    /*

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

        return a + b + c;

    }

    */

 

    // 两个整数的求和

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

        return a + b;

    }

}


方法重载练习之比较数据是否相等



package com.itheima_03;

 

/*

 * 需求:比较两个数据是否相等。参数类型分别为两个byte类型,两个short类型,两个int类型,两个long类型,

 *     并在main方法中进行测试

 */

public class MethodTest {

    public static
void main(String[] args) {

        // 调用

        System.out.println(compare(10, 20));

        System.out.println("-------------");

        System.out.println(compare((byte)10, (byte)20));

        System.out.println("-------------");

        System.out.println(compare((short)10, (short)20));

        System.out.println("-------------");

        //System.out.println(compare((long)10, (long)20));

        System.out.println(compare(10L, 20L));

    }

 

    // 两个byte类型的

    public static
boolean compare(byte a, byte b) {

        System.out.println("byte");

        // 第一种写法

        // boolean flag = a==b?true:false;

        // return flag;

        // 第二种写法

        // boolean flag = a == b;

        // return flag;

        // 第三种写法

        return a ==
b;

    }

 

    // 两个short类型的

    public static
boolean compare(short a, short b) {

        System.out.println("short");

        return a ==
b;

    }

 

    // 两个int类型的

    public static
boolean compare(int a, int b) {

        System.out.println("int");

        return a ==
b;

    }

 

    // 两个long类型的

    public static
boolean compare(long a, long b) {

        System.out.println("long");

        return a ==
b;

    }

}

 


方法的形式参数为基本数据类型

方法的参数是基本类型的时候:

         形式参数的改变不影响实际参数。

形式参数:用于接收实际数据的变量

实际参数:实际参与运算的变量



public class ArgsDemo {

    public static
void main(String[] args) {

        // 定义变量

        int a = 10;

        int b = 20;

        System.out.println("a:"
+ a + ",b:"
+ b);//
a:10,b:20

        change(a, b);

        System.out.println("a:"
+ a + ",b:"
+ b);//
a:10,b:20

    }

 

    public static
void change(int a, int b) { // a=10,b=20

        System.out.println("a:"
+ a + ",b:"
+ b);//
a:10,b:20

        a = b; // a=20;

        b = a + b;// b=40;

        System.out.println("a:"
+ a + ",b:"
+ b);//
a:20,b:40

    }

 

}

 

 


方法的形式参数基本类型图解
在这里插入图片描述

方法的形式参数为引用数据类型



package com.itheima_04;

 

/*

 * 方法的参数是引用类型:

 *      形式参数的改变直接影响实际参数

 */

public class ArgsDemo2 {

    public static
void main(String[] args) {

        // 定义数组

        int[] arr =
{ 1, 2, 3, 4, 5 };

        // 遍历数组

        for (int
x = 0; x
< arr.length;
x++) {

            System.out.println(arr[x]);

        }

        System.out.println("----------------");

        change(arr);

        for (int
x = 0; x
< arr.length;
x++) {

            System.out.println(arr[x]);

        }

    }

 

    public static
void change(int[] arr)
{

        for (int
x = 0; x
< arr.length;
x++) {

            // 如果元素是偶数,值就变为以前的2倍

            if (arr[x] % 2 == 0) {

                arr[x] *= 2;

            }

        }

    }

}


在这里插入图片描述

方法的操作数组的练习

需求:把遍历数组改进为方法实现,并调用方法



package com.itheima_05;

 

/*

 * 需求:把遍历数组改进为方法实现,并调用方法

 */

public class MethodTest {

    public static
void main(String[] args) {

        // 定义数组

        int[] arr =
{ 11, 22, 33, 44, 55 };

 

        // 遍历

        // for (int x = 0; x < arr.length; x++) {

        // System.out.println(arr[x]);

        // }

        

        //用方法改进

        //printArray(arr);

        

        //这一次虽然可以,但是我觉得格式不好看,能不能打印成下面的格式呢?

        //[元素1, 元素2, 元素3,
...]

        printArray(arr);

    }

    

    public static
void printArray(int[] arr)
{

        System.out.print("[");

        for(int
x=0; x<arr.length; x++){

            if(x==arr.length-1){

                System.out.println(arr[x]+"]");

            }else {

                System.out.print(arr[x]+", ");

            }

        }

    }

    

    /*

     * 两个明确:

     *      返回值类型:void

     *      参数列表:int[] arr

     */

//  public static void printArray(int[] arr) {

//      for(int x=0; x<arr.length; x++){

//          System.out.println(arr[x]);

//      }

//  }

}

 


方法的练习之数组获取最值



/*

 * 需求:把获取数组最值改进为方法实现,并调用方法

 */

public class MethodTest2 {

    public static void main(String[] args) {

        // 定义数组

        int[] arr = { 34, 67, 10, 28, 59 };

        

        //获取数组中的最大值

        // //定义参照物

        // int max = arr[0];

        // //遍历,依次比较,大的留下来

        // for(int x=1;
x<arr.length; x++) {

        // if(arr[x] > max) {

        // max = arr[x];

        // }

        // }

        

        //用方法改进

        int max = getMax(arr);

        System.out.println("max:"+max);

        

        //获取数组中的最小值,用方法实现

        int min = getMin(arr);

        System.out.println("min:"+min);

        

    }

    

    //获取数组中的最小值的方法

    public static int getMin(int[] arr)
{

        int min = arr[0];

        

        for(int x=1; x<arr.length; x++) {

            if(arr[x] < min) {

                min = arr[x];

            }

        }

        

        return min;

    }

    

    /*

     * 两个明确:

     *      返回值类型:int

     *      参数列表:int[]
arr

     */

    public static int getMax(int[] arr)
{

        int max = arr[0];

        

        for(int x=1; x<arr.length; x++) {

            if(arr[x] > max) {

                max = arr[x];

            }

        }

        

        return max;

    }

}


方法的练习之数组元素求和



package com.itheima_05;

 

/*

 * 需求:写一个方法,用于对数组进行求和,并调用方法。

 */

public class MethodTest3 {

    public static
void main(String[] args) {

        // 定义数组

        int[] arr =
{ 1, 2, 3, 4, 5 };

        

        // //定义求和变量

        // int sum = 0;

        // //获取数组中的每一个元素

        // for(int x=0; x<arr.length; x++) {

        // sum += arr[x];

        // }

        

        //用方法改进

        int sum = sum(arr);

        

        System.out.println("sum:"+sum);

    }

    

    /*

     * 两个明确:

     *      返回值类型:int

     *      参数列表:int[] arr

     */

    public static
int sum(int[]
arr) {

        int sum =
0;

        

        for(int
x=0; x<arr.length; x++) {

            sum += arr[x];

        }

        

        return sum;

    }

}

 

 

 


猜你喜欢

转载自blog.csdn.net/qq_42733916/article/details/86408689