首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Java 基礎教程:建構子與方法多載完整解析

     編輯:Java 程式設計入門

建構子

物件建立時自動呼叫,用於初始化資料成員與執行特定操作。

特徵

  • 名稱與類別相同
  • 無回傳類型(連 void 都沒有)
  • 可接收參數列表
  • 透過 new ClassName(args) 呼叫

基本範例


public class Human {
    int height;
    
    // 建構子
    public Human(int h) {
        this.height = h;
        System.out.println("I'm born, height: " + h);
    }
    
    public int getHeight() { return height; }
}

public class Test {
    public static void main(String[] args) {
        Human person = new Human(160);  // 呼叫建構子
        System.out.println(person.getHeight());  // 160
    }
}

輸出:


I'm born, height: 160
160

建構子做兩件事

  1. 為資料成員提供初始值:this.height = h;
  2. 執行特定初始化操作:System.out.println(...)

初始化優先順序

三種初始化方式優先級(高 → 低):


建構子 > 顯式初始化 > 預設初始化

驗證範例


class Human {
    int height = 170;  // 顯式初始化
    
    public Human(int h) {
        this.height = h;  // 建構子覆蓋
    }
}

Human h = new Human(160);
System.out.println(h.height);  // 160(建構子勝出)

預設初始值

類型預設值
數值型0
布林值false
物件參考null

方法多載

同一類別中定義多個同名方法,參數列表不同(類型、個數、順序)。編譯器根據呼叫時的實參決定呼叫哪個方法。

建構子多載


class Human {
    int height;
    String name;
    
    // 建構子 1
    public Human(int h) {
        this.height = h;
        System.out.println("建構子1: height=" + h);
    }
    
    // 建構子 2
    public Human(int h, String n) {
        this.height = h;
        this.name = n;
        System.out.println("建構子2: height=" + h + ", name=" + n);
    }
}

Human h1 = new Human(150);           // 呼叫建構子1
Human h2 = new Human(150, "NeZha");  // 呼叫建構子2

普通方法多載


class Human {
    void breath() {
        System.out.println("hu...hu...");
    }
    
    void breath(int rep) {
        for (int i = 0; i < rep; i++) {
            System.out.println("lu...lu...");
        }
    }
}

Human h = new Human();
h.breath();       // 呼叫無參數版本
h.breath(10);     // 呼叫有參數版本,輸出 10 行

多載解析規則

  1. 精確匹配參數類型
  2. 隱式類型轉換(widening):int → long → float → double
  3. 自動裝箱/拆箱
  4. 可變參數 (...) 優先級最低

多載陷阱


void test(int i) { System.out.println("int"); }
void test(long l) { System.out.println("long"); }
void test(Integer i) { System.out.println("Integer"); }

test(1);        // int(精確匹配)
test(1L);       // long
test(new Integer(1)); // Integer

// 陷阱:null 無法確定類型
// test(null);  // 編譯錯誤:ambiguous
test((Integer) null); // 需強制轉型

this 關鍵字

  • this.field:存取實例欄位(區分同名參數)
  • this.method():呼叫實例方法
  • this(...):建構子鏈呼叫另一個建構子(必須在第一行)
  • 隱性參數:每個實例方法隱含接收 this

建構子鏈範例


class Human {
    int height;
    String name;
    int age;
    
    public Human(int height) {
        this(height, "Unknown");  // 呼叫兩參數建構子
    }
    
    public Human(int height, String name) {
        this(height, name, 0);    // 呼叫三參數建構子
    }
    
    public Human(int height, String name, int age) {
        this.height = height;
        this.name = name;
        this.age = age;
        System.out.println("完整初始化: " + height + ", " + name + ", " + age);
    }
}

new Human(160);              // 完整初始化: 160, Unknown, 0
new Human(160, "Tom");       // 完整初始化: 160, Tom, 0
new Human(160, "Tom", 25);   // 完整初始化: 160, Tom, 25

總結

概念關鍵點
建構子同名、無回傳、new 時呼叫、初始化用
初始化優先級建構子 > 顯式 > 預設
方法多載同名、不同參數列表、編譯期綁定
this實例參考、區分同名、建構子鏈

練習建議

  1. 建立 Rectangle 類別,提供 3 個建構子(寬高、寬高顏色、點座標寬高)
  2. 實作 Calculator 類別,多載 add 方法支援 int、double、int[]、List<Integer>
  3. 觀察 JDK 原始碼中 String、ArrayList、Thread 的建構子多載設計
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved