比方说,我有多个构造一类,其中之一是一个拷贝构造函数(复制对象):Java的空参数时链构造
public class Rectangle {
int width, height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public Rectangle(Rectangle source) {
this(source.width, source.height);
}
}
有没有什么办法可以让检查source
是null
在复制构造函数中,并且如果它是?IllegalArgumentException
?因为其他构造函数调用已将作为我构造函数中的第一个语句。
为什么其他构造函数调用必须是复制构造函数中的第一条语句? – Janno
因为这就是Java所需要的。 – kalsowerus
@Janno:因为这就是Java的工作原理。在另一个陈述之后不能使用this(...)。 –