方法是 Java 程式碼復用的基本單元,將具體業務邏輯封裝為可調用的功能塊。本文系統梳理方法的定義語法、核心特性、調用機制、參數傳遞、返回值處理及重載規則,並配以豐富示例與練習,幫助初學者建立扎實的方法設計思維。
[修飾符] 返回值類型 方法名(參數列表) {
// 方法體:執行語句
return 返回值; // void 類型可省略
}
| 術語 | 說明 |
|---|---|
| 修飾符 | public、private、protected、static、final 等,控制可見性與行為 |
| 返回值類型 | 方法執行後產生的數據類型;無具體返回值用 void |
| 方法名 | 標識符,遵循駝峰命名,動詞開頭(如 calculateSum) |
| 形式參數 | 方法簽名中聲明的變量,接收調用者傳入的實際值 |
| 實際參數 | 調用時傳入的具體數值或變量 |
| return | 結束方法執行並將值返回給調用者;void 方法可省略或單獨寫 return; |
return; 可省略(編譯器自動在方法末尾補齊)。Java 只有值傳遞:
public class ParamDemo {
public static void main(String[] args) {
int a = 10;
changePrimitive(a);
System.out.println("基本類型調用後 a=" + a); // 10,未變
StringBuilder sb = new StringBuilder("Hello");
changeReference(sb);
System.out.println("引用類型調用後 sb=" + sb); // Hello World,內部狀態變了
StringBuilder sb2 = new StringBuilder("Test");
reassignReference(sb2);
System.out.println("重新賦值引用後 sb2=" + sb2); // Test,原引用未變
}
static void changePrimitive(int x) { x = 20; }
static void changeReference(StringBuilder s) { s.append(" World"); }
static void reassignReference(StringBuilder s) { s = new StringBuilder("Changed"); }
}
同一類中允許存在多個同名方法,只要參數列表不同(個數、類型、順序任一不同)即可。與返回值類型、修飾符無關。
| 原方法 | 新方法 | 是否重載 | 原因 |
|---|---|---|---|
void show(int a, char b, double c) | void show(int x, char y, double z) | ❌ | 參數列表完全相同(名字不算) |
void show(int a, char b, double c) | int show(int a, double c, char b) | ✅ | 參數類型順序不同 |
void show(int a, char b, double c) | void show(int a, double c, char b) | ✅ | 參數類型順序不同 |
void show(int a, char b, double c) | boolean show(int c, char b) | ✅ | 參數個數不同 |
void show(int a, char b, double c) | void show(double c) | ✅ | 參數個數不同 |
void show(int a, char b, double c) | double show(int x, char y, double z) | ❌ | 僅返回值不同,參數列表相同 → 編譯錯誤 |
add(int, int)、add(double, double)、add(int, int, int))。public class MathUtils {
// 核心實現
public static int add(int a, int b) { return a + b; }
// 重載:三個整數求和,復用雙參數版本
public static int add(int a, int b, int c) { return add(add(a, b), c); }
// 重載:雙精度浮點數求和
public static double add(double a, double b) { return a + b; }
// 避免:參數類型隱式轉換導致歧義
// public static long add(long a, long b) { ... } // 與 int 版本可能產生調用歧義
}
public static void printRect(int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) System.out.print("*");
System.out.println();
}
}
void(直接打印,無計算結果)。int(行、列)。public static void print99() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (j*i) + "\t");
}
System.out.println();
}
}
// 重載版:可指定最大乘數
public static void print99(int max) {
for (int i = 1; i <= max; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (j*i) + "\t");
}
System.out.println();
}
}
public static int max(int a, int b) {
return (a > b) ? a : b; // 三元運算符簡化邏輯
}
public static void main(String[] args)。args:命令行參數數組,args[0] 為第一個參數。calculateTotal、findUserById、saveOrder。is/has/can 開頭,如 isValid、hasPermission。方法是構建 Java 程序的基石。掌握「兩個決策(返回值、參數列表)」、「值傳遞機制」、「重載規則(只看參數列表)」,並養成「單一職責、語義命名、適度重載」的編碼習慣,能寫出清晰、可維護、可復用的代碼。練習中多思考:這個功能的結果是什么?需要哪些外部輸入?——這就是方法設計的核心思維。