java方法的参数类型_Java 基础 14 方法的重载 与 方法参数类型详解

1.1 方法重载的概述和特点

方法重载概述

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

方法重载特点

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

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

1.1.1 案例代码

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);

int result2 = sum(a,b,c);

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

}

/*

public static int sum(int x,int y) {

return x + y;

}

*/

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

return a + b;

}

//求三个数据的和

/*

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

return a + b + c;

}

*/

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

return a + b + c;

}

//求两个数据的和

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

/*

int c = a + b;

return c;

*/

return a + b;

}

}

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

1.2.1代码案例

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(compare((byte)10, (byte)20));

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

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

}

/*

* 两个byte类型

*

* 两个明确:

* 返回值类型:boolean

* 参数列表:byte a,byte b

*/

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

/*

if(a == b) {

return true;

}else {

return false;

}

*/

System.out.println("byte");

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;

}

}

2.1 方法中参数传递

2.1.1 方法参数是基本类型的情况和图解

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

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

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

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

2.1.1.1 代码案例

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

}

}

2.1.1.2方法的形式参数是基本类型图解

cdfd4e5caf97

Parameter_Diagram.png

2.1.2 方法参数是引用类型的情况和图解

2.1.2.1代码案例

package com.itheima;

/*

* 基本数据类型作为参数传递,形式参数的改变不影响实际参数

* 引用数据类型作为参数传递,形式参数的改变直接影响实际参数

*/

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]);//1,2,3,4,5

}

change(arr);

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

System.out.println(arr[x]);//1,4,3,8,5

}

}

public static void change(int[] arr) {

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

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

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

arr[x] *= 2;

}

}

}

}

2.1.2.2方法的形式参数是引用类型图

cdfd4e5caf97

Parameter_Diagram01.png

2.2 方法操作数组练习

2.2.1方法练习之数组遍历

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

2.2.2代码案例

package com.itheima;

/*

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

*/

public class MethodTest {

public static void main(String[] args) {

//定义数组

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

//调用方法

//printArray(arr);

//我们已经实现了需求,但是我觉得这个效果不好看,我想要如下的效果可以吗

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

//我们的结果应该是这样的:[11, 22, 33, 44, 55]

printArray(arr);

}

/*

* 遍历数组的方法。

*

* 两个明确:

* 返回值类型:void

* 参数列表:int[] arr

*/

/*

public static void printArray(int[] arr) {

for(int x=0; x

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

}

}

*/

public static void printArray(int[] arr) {

System.out.print("[");

for(int x=0; x

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

//判断是否是最后一个元素

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

}else {

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

}

}

System.out.println("]");

}

}

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

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

2.2.4 代码案例

package com.itheima;

/*

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

*/

public class MethodTest2 {

public static void main(String[] args) {

//定义数组

int[] arr = {24,36,90,75,81};

//调用方法

int max = getMax(arr);

//输出结果

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

}

/*

* 数组获取最值的方法

*

* 两个明确:

* 返回值类型:int

* 参数列表:int[] arr

*/

public static int getMax(int[] arr) {

//定义参照物

int max = arr[0];

//遍历,获取元素,进行比较

for(int x=1; x

if(arr[x] > max) {

max = arr[x];

}

}

return max;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值