1. 程式人生 > >第3章 方法的重載及參數傳遞

第3章 方法的重載及參數傳遞

Java

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

1.2 方法重載練習1.2.1 方法重載練習之比較數據是否相等1.2.2 代碼案例十一

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

1.3 方法中參數傳遞1.3.1 方法的形式參數為基本數據類型
方法的參數是基本類型的時候:
形式參數的改變不影響實際參數。
形式參數:用於接收實際數據的變量
實際參數:實際參與運算的變量
1.3.2 代碼案例十二

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
}

}

1.3.3 方法的形式參數是基本類型圖解
技術分享圖片
1.3.4 方法的形式參數為引用數據類型1.3.5 代碼案例十三

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

1.3.6 方法的形式參數是引用類型圖
技術分享圖片

第3章 方法的重載及參數傳遞