Java 构造方法:定义、重载与调用
一、什么是构造方法
构造方法是在使用 new 创建对象时自动调用的方法,用来给对象的成员变量赋初值。
点击查看代码
class Student {
String name;
int age;
Student() {
name = "未知";
age = 18;
}
}
// 使用
Student stu = new Student(); // 自动调用 Student()
语法特征
| 特征 | 说明 |
|---|---|
| 方法名与类名完全一致 | 区分大小写 |
| 没有返回值 | 连 void 都不写 |
| 创建对象时自动调用 | 不能手动调用 |
| 不写也会存在 | 编译器自动生成无参构造 |
二、默认构造方法
如果类中没有写任何构造方法,编译器会自动补一个无参构造:
点击查看代码
class Student {
String name;
int age;
}
// 等价于编译器自动补上:
// Student() {}
所以 new Student() 不会报错。
一旦写了任何构造方法,编译器就不再自动生成无参构造:
点击查看代码
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
}
Student s1 = new Student("小明", 20); // ✅
Student s2 = new Student(); // ❌ 报错:找不到无参构造
结论:写了有参构造后,如果还想用无参的,必须手动补上。
三、构造方法重载
构造方法可以重载——同一个类中存在多个同名(与类名相同)但参数列表不同的构造方法。
3.1 重载规则
| 区分方式 | 是否合法 |
|---|---|
| 参数个数不同 | ✅ |
| 参数类型不同 | ✅ |
| 参数顺序不同 | ✅ |
| 仅修饰符不同 | ❌ 视为重复 |
| 仅返回值不同 | ❌ 构造方法无返回值,此情况不存在 |
3.2 完整示例
点击查看代码
public class Student01 {
String stuID;
String stuName;
String stuBirth;
String stuClass;
// 无参构造
Student01() {
stuID = "未分配";
stuName = "未知";
stuBirth = "未知";
stuClass = "未分配";
}
// 单参构造
Student01(String stuID) {
this.stuID = stuID;
}
// 两参构造
Student01(String stuID, String stuName) {
this.stuID = stuID;
this.stuName = stuName;
}
// 四参构造
Student01(String stuID, String stuName, String stuBirth, String stuClass) {
this.stuID = stuID;
this.stuName = stuName;
this.stuBirth = stuBirth;
this.stuClass = stuClass;
}
}
3.3 调用
点击查看代码
Student01 s1 = new Student01(); // 调用无参
Student01 s2 = new Student01("001"); // 调用单参
Student01 s3 = new Student01("002", "李四"); // 调用两参
Student01 s4 = new Student01("003", "王五", "2005", "二班"); // 调用四参
编译器根据 new 后面的参数个数和类型自动匹配对应的构造方法。
四、this 在构造方法中的使用
4.1 区分成员变量与参数
当参数名与成员变量名相同时,必须用 this 区分:
点击查看代码
Student01(String stuID, String stuName) {
this.stuID = stuID; // this.stuID 是成员变量,stuID 是参数
this.stuName = stuName;
}
如果不写 this:
点击查看代码
Student01(String stuID) {
stuID = stuID; // ❌ 两个都是参数,成员变量根本没被赋值
}
4.2 构造方法之间互相调用
使用 this(...) 可以在一个构造方法中调用另一个构造方法,且必须是方法体的第一行:
点击查看代码
Student01() {
this("未分配", "未知", "未知", "未分配"); // 调用四参构造
}
Student01(String stuID) {
this(stuID, "未知", "未知", "未分配"); // 调用四参构造
}
Student01(String stuID, String stuName) {
this(stuID, stuName, "未知", "未知"); // 调用四参构造
}
Student01(String stuID, String stuName, String stuBirth, String stuClass) {
this.stuID = stuID;
this.stuName = stuName;
this.stuBirth = stuBirth;
this.stuClass = stuClass;
}
好处:避免重复代码,所有初始化逻辑集中在最完整的构造方法中,其余通过 this(...) 转发。
五、构造方法的权限修饰
构造方法也可以用权限修饰符控制谁能创建对象:
| 修饰符 | 效果 | 典型用途 |
|---|---|---|
| public | 任何地方可 new | 普通类 |
| 默认 | 同包可 new | 包内工具类 |
| protected | 同包 + 子类可 new | 框架设计 |
| private | 仅本类可 new | 单例模式 |
点击查看代码
public class Student01 {
private Student01() { } // 外部无法 new
public static Student01 create() {
return new Student01(); // 通过静态方法控制创建
}
}
六、常见错误汇总
| 错误 | 原因 | 修正 |
|---|---|---|
| 两个参数列表一样的构造方法 | 重载只看参数列表,不看修饰符 | 删除重复或改参数 |
| 写了有参构造后无参 new 报错 | 编译器不再自动生成无参构造 | 手动补一个无参构造 |
| 构造里 stuID = stuID 没效果 | 参数遮蔽了成员变量 | 加 this. |
| this(...) 不在第一行 | 语法强制 | 移到第一行 |
| 构造方法写返回值类型 | 构造方法没有返回值 | 去掉返回值类型 |
| 试图用外部对象名给成员变量赋值 | 构造方法里访问不到外部变量 | 用 this. |
七、核心结论
1.构造方法名与类名一致,无返回值,创建对象时自动调用。
2.不写构造方法时编译器自动补无参构造;写了任何构造后不再自动补。
3.构造方法可以重载,通过参数列表区分,编译器自动匹配。
4.this.成员变量 用于区分重名的参数与成员变量——同名时唯一解。
5.this(...) 可在构造方法间转发调用,必须放第一行。
6.构造方法也可用权限修饰符控制实例化范围。

浙公网安备 33010602011771号