全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-08 6 分钟 ✍️ juanwangdev

构造方法

构造方法是创建对象时调用的特殊方法。

构造方法概念

什么是构造方法

构造方法用于初始化对象,在创建对象时自动调用。

Java
public class Person {
    String name;
    int age;
    
    // 构造方法
    public Person() {
        name = "未命名";
        age = 0;
    }
    
    // 有参构造方法
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

// 创建对象时调用构造方法
Person p1 = new Person();           // 调用无参构造
Person p2 = new Person("张三", 25);  // 调用有参构造

构造方法特点

  • 方法名与类名相同
  • 无返回值(不是void)
  • 创建对象时自动调用
  • 可重载(多个构造方法)

构造方法语法

无参构造方法

Java
public class Student {
    private String name;
    private int age;
    
    // 无参构造方法
    public Student() {
        name = "默认名";
        age = 18;
    }
}

Student s = new Student();  // 自动调用无参构造
System.out.println(s.name);  // 输出:默认名

有参构造方法

Java
public class Student {
    private String name;
    private int age;
    
    // 有参构造方法
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

// 创建对象并初始化
Student s = new Student("张三", 20);
System.out.println(s.name);  // 输出:张三

默认构造方法

自动提供无参构造

如果类没有定义任何构造方法,编译器自动提供无参构造。

Java
public class Person {
    String name;
    int age;
    // 未定义构造方法,编译器自动提供:
    // public Person() { }
}

Person p = new Person();  // 使用自动提供的无参构造

定义构造方法后默认消失

一旦定义了构造方法,编译器不再提供默认构造。

Java
public class Person {
    String name;
    
    // 定义了有参构造
    public Person(String name) {
        this.name = name;
    }
    
    // 默认无参构造不再存在
}

// 错误:无参构造不存在
Person p = new Person();  // 编译错误

// 正确:使用有参构造
Person p = new Person("张三");  // 正确

如果需要无参构造,必须显式定义。

构造方法重载

多个构造方法

一个类可以定义多个构造方法,参数不同。

Java
public class Student {
    private String name;
    private int age;
    private double score;
    
    // 无参构造
    public Student() {
        name = "默认";
        age = 18;
        score = 0;
    }
    
    // 一个参数
    public Student(String name) {
        this.name = name;
        age = 18;
        score = 0;
    }
    
    // 两个参数
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
        score = 0;
    }
    
    // 三个参数
    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
}

// 使用不同构造方法
Student s1 = new Student();
Student s2 = new Student("张三");
Student s3 = new Student("李四", 20);
Student s4 = new Student("王五", 21, 85.5);

构造方法调用

this调用本类其他构造

使用this()调用本类其他构造方法,必须在第一行。

Java
public class Student {
    private String name;
    private int age;
    private double score;
    
    // 主构造方法
    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    
    // 调用主构造
    public Student(String name, int age) {
        this(name, age, 0);  // 调用三参数构造
    }
    
    public Student(String name) {
        this(name, 18, 0);  // 调用三参数构造
    }
    
    public Student() {
        this("默认", 18, 0);  // 调用三参数构造
    }
}

this()必须在构造方法第一行。

继承中的构造方法

super调用父类构造

子类构造方法默认调用父类无参构造super()。

Java
public class Animal {
    String name;
    
    // 父类无参构造
    public Animal() {
        name = "动物";
    }
    
    // 父类有参构造
    public Animal(String name) {
        this.name = name;
    }
}

public class Dog extends Animal {
    // 默认调用super()(父类无参构造)
    public Dog() {
        // super();  // 自动调用(可省略)
    }
    
    // 调用父类有参构造
    public Dog(String name) {
        super(name);  // 必须显式调用
    }
}

super必须在第一行

Java
public class Dog extends Animal {
    public Dog(String name) {
        super(name);  // 第一行
        // 其他初始化代码...
    }
    
    // 错误:super不在第一行
    public Dog(int age) {
        // some code...    // 编译错误
        super("狗");       // 必须第一行
    }
}

构造方法与初始化顺序

初始化执行顺序

  1. 父类静态初始化
  2. 子类静态初始化
  3. 父类构造方法
  4. 子类构造方法
Java
public class Parent {
    public Parent() {
        System.out.println("父类构造");
    }
}

public class Child extends Parent {
    public Child() {
        System.out.println("子类构造");
    }
}

Child c = new Child();
// 输出:父类构造 → 子类构造

私有构造方法

单例模式

私有构造方法防止外部创建对象。

Java
public class Singleton {
    private static Singleton instance;
    
    // 私有构造方法
    private Singleton() { }
    
    // 公共获取方法
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

// 外部无法直接创建
Singleton s = Singleton.getInstance();  // 通过方法获取

构造方法最佳实践

推荐做法

  • 提供无参构造(便于框架使用)
  • 主构造方法参数最多
  • 其他构造调用主构造
  • 构造方法中完成必要初始化
Java
public class User {
    private Long id;
    private String name;
    private String email;
    
    // 主构造(全参数)
    public User(Long id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }
    
    // 简化构造(调用主构造)
    public User(String name) {
        this(null, name, null);
    }
    
    // 无参构造
    public User() {
        this(null, null, null);
    }
}

要点总结

  • 构造方法用于初始化对象
  • 方法名与类名相同,无返回值
  • 创建对象时自动调用(new)
  • 无参构造和有参构造可重载
  • 未定义构造时自动提供无参构造
  • 定义构造后默认构造消失
  • this()调用本类其他构造(第一行)
  • super()调用父类构造(第一行)
  • 子类构造默认调用父类无参构造
  • 私有构造用于单例模式

📝 发现内容有误?点击此处直接编辑

← 上一篇 封装
下一篇 → 类与对象
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库