【发布时间】:2020-01-28 15:39:07
【问题描述】:
比如说我有一个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) {
// ???
}
ColoredCircle 的第二个构造函数应该输入什么?
this(centre, color, Circle.DEFAULT_RADIUS);super(centre, Circle.DEFAULT_RADIUS); this.color = color;
我认为任何一个都可以,但哪个会产生“更干净的代码”?
【问题讨论】:
-
如果不使用继承而使用接口,生活可能会更轻松
标签: java inheritance this super