[英]Calling a base class constructor from derived class in Java
I have a class as follows: 我有一个课程如下:
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;
}
}
Now a regular polygon is a polygon whose all sides are equal. 现在,正多边形是一个多边形,其边是相等的。 What should be the constructor of my regular polygon? 什么应该是我的正多边形的构造函数?
public Regularpolygon extends Polygon{
//constructor ???
}
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));
}
}
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);
}
}
Your constructor should be 你的构造函数应该是
public Regularpolygon extends Polygon{
public Regularpolygon (int id,Point center,int noSides,int lengths[]){
super(id, center,noSides,lengths[]);
// YOUR CODE HERE
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.