Java 构造方法
构造方法的类型、重载、this 关键字与初始化顺序。
构造方法的类型
Constructors are either no-arg or parameterised. No-arg constructors let frameworks instantiate reflectively; parameterised ones initialise in one step. They cannot be static, final or abstract.
public class Student {
String name;
int age;
public Student() {
this.name = "unknown";
this.age = 0;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Student a = new Student();
Student b = new Student("Tom", 20);
System.out.println(a.name + " " + a.age);
System.out.println(b.name + " " + b.age);
}
}unknown 0
Tom 20构造方法的重载
Several constructors with different parameter lists are overloads. The common pattern has shorter ones delegate to the fullest via this(...), so initialisation logic lives in one place.
public class Box {
int w;
int h;
String label;
public Box() {
this(1, 1, "default");
}
public Box(int w, int h) {
this(w, h, "box");
}
public Box(int w, int h, String label) {
this.w = w;
this.h = h;
this.label = label;
}
public static void main(String[] args) {
System.out.println(new Box().label);
System.out.println(new Box(2, 3).label);
System.out.println(new Box(4, 5, "big").w);
}
}default
box
4构造方法中的 this 关键字
this refers to the current object. It most often disambiguates a parameter shadowing a field: this.name = name. this(...) calls another constructor of the same class and must be the first statement.
public class Employee {
String name;
double salary;
public Employee(String name) {
this(name, 5000);
}
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public Employee self() {
return this;
}
public static void main(String[] args) {
Employee e = new Employee("Tom");
System.out.println(e.name + " " + e.salary);
System.out.println(e.self() == e);
}
}Tom 5000.0
truesuper(). If the parent has only parameterised constructors, the subclass must call super(args) explicitly or it will not compile.构造方法的类型
构造方法分无参和有参两类。无参构造常用于框架反射创建对象;有参构造用于一次性完成初始化。构造方法不能被 static、final、abstract 修饰。
public class Student {
String name;
int age;
public Student() {
this.name = "unknown";
this.age = 0;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Student a = new Student();
Student b = new Student("Tom", 20);
System.out.println(a.name + " " + a.age);
System.out.println(b.name + " " + b.age);
}
}unknown 0
Tom 20构造方法的重载
多个构造方法参数列表不同即为重载。常用做法是让参数少的构造用 this(...) 委托给参数最全的那个,避免初始化逻辑重复。
public class Box {
int w;
int h;
String label;
public Box() {
this(1, 1, "default");
}
public Box(int w, int h) {
this(w, h, "box");
}
public Box(int w, int h, String label) {
this.w = w;
this.h = h;
this.label = label;
}
public static void main(String[] args) {
System.out.println(new Box().label);
System.out.println(new Box(2, 3).label);
System.out.println(new Box(4, 5, "big").w);
}
}default
box
4构造方法中的 this 关键字
this 指向当前对象。最常见的用法是解决「参数名与字段名同名」的遮蔽问题:this.name = name。this(...) 则用于调用同类另一个构造方法,必须是第一句。
public class Employee {
String name;
double salary;
public Employee(String name) {
this(name, 5000);
}
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public Employee self() {
return this;
}
public static void main(String[] args) {
Employee e = new Employee("Tom");
System.out.println(e.name + " " + e.salary);
System.out.println(e.self() == e);
}
}Tom 5000.0
truesuper()(调用父类无参构造)。如果父类只有带参构造,子类必须显式写 super(参数),否则编译失败。