我有一个课程如下:
public class Polygon extends Shape{
private int noSides;
private int lenghts[];
public Polygon(int id,Point center,int noSides,int lengths[]) {
super(id, center);
this.noSides = noSides;
this.lenghts = lengths;
}
}
现在,正多边形是一个多边形,其边是相等的。什么应该是我的正多边形的构造函数?
public Regularpolygon extends Polygon{
//constructor ???
}
答案 0 :(得分:54)
public class Polygon extends Shape {
private int noSides;
private int lenghts[];
public Polygon(int id,Point center,int noSides,int lengths[]) {
super(id, center);
this.noSides = noSides;
this.lenghts = lengths;
}
}
public class RegularPolygon extends Polygon {
private static int[] getFilledArray(int noSides, int length) {
int[] a = new int[noSides];
java.util.Arrays.fill(a, length);
return a;
}
public RegularPolygon(int id, Point center, int noSides, int length) {
super(id, center, noSides, getFilledArray(noSides, length));
}
}
答案 1 :(得分:2)
你的构造函数应该是
public Regularpolygon extends Polygon{
public Regularpolygon (int id,Point center,int noSides,int lengths[]){
super(id, center,noSides,lengths[]);
// YOUR CODE HERE
}
}
答案 2 :(得分:2)
class Foo {
Foo(String str) { }
}
class Bar extends Foo {
Bar(String str) {
// Here I am explicitly calling the superclass
// constructor - since constructors are not inherited
// you must chain them like this.
super(str);
}
}