例如,我有一个Circle
类:
static final double DEFAULT_RADIUS = 1.0;
Circle(Point centre, double radius) {
this.centre = centre;
this.radius = radius;
}
Circle(Point centre) {
this(centre, Circle.DEFAULT_RADIUS);
}
// ...
然后在ColoredCircle
中,为Circle
的子类:
ColoredCircle(Point centre, Color color, double radius) {
super(centre, radius);
this.color = color;
}
ColoredCircle(Point centre, Color color) {
// ???
}
[C0的第二个构造函数应该输入什么?
ColoredCircle
this(centre, color, Circle.DEFAULT_RADIUS);
我认为任何一种都可以,但是会导致“更干净的代码”?
您的两个示例都有点多余,因为super(centre, Circle.DEFAULT_RADIUS); this.color = color;
已经具有将半径设置为默认值的构造函数。
我建议您使用它,并像这样构造您的构造函数:
Circle