小编sad*_*iwi的帖子

重载子类的构造函数

比如说我有一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);
}

// ...
Run Code Online (Sandbox Code Playgroud)

然后在ColoredCircle,一个子类Circle

ColoredCircle(Point centre, Color color, double radius) {
    super(centre, radius);
    this.color = color;
}

ColoredCircle(Point centre, Color color) {
    // ???
}
Run Code Online (Sandbox Code Playgroud)

的第二个构造函数应该ColoredCircle怎么做?

  • this(centre, color, Circle.DEFAULT_RADIUS);
  • super(centre, Circle.DEFAULT_RADIUS); this.color = color;

我认为两者都可以,但这会导致“更干净的代码”?

java inheritance this super

0
推荐指数
1
解决办法
63
查看次数

标签 统计

inheritance ×1

java ×1

super ×1

this ×1