所以我在java中为学校做了一个任务...这是一个类层次结构类型的任务,我们应该创建一个“Triangle.java”类,该类扩展了一个“ClosedShape.java”类,该类扩展了“Shape.java”.... ClosedShape和Shape都给予了我们,所以它们很可能没有错(我'反正会后他们),但我的三角类如下:构造函数...不适用(实际和正式参数列表的长度不同) - 但它确实匹配
public abstract class Triangle extends ClosedShape{
public Triangle(int[] a, int[] b, int base, int height){
super(true, 3, a, b);
setWidth(base);
setHeight(height);
setXYCoords(a, b);
}
public Triangle(int x, int y, int base, int height){
int[] a = new int[3];
int[] b = new int[3];
a[0] = x;
a[1] = (x+base)/2;
a[2] = (x+base);
b[0] = y;
b[1] = (y+height)/2;
b[2] = (y+height);
}
}
我之所以有两个构造是因为我需要创建这两个数组来保存点绘制形状......然后我需要让它们传入ClosedShape(boolean,int,int [],int [])超类......如果我在调用super()之前,需要在同一个构造函数中创建数组,以便它们可以被传入,但这是不允许的,因为对super()的调用必须是第一个...所以当前,当我尝试编译Triangle.java我得到的错误:
Triangle.java.14: error: no suitable constructor found for ClosedShape()
{ //little arrow pointing under the '{'
constructor ClosedShape.ClosedShape(boolean, int, int[], int[]) is not applicable
(actual and formal argument lists differ in length)
constructor ClosedShape.ClosedShape(boolean, int) is not applicable
(actual and formal argument lists differ in length)
1 error
还指定了分配,对于三角形签名必须是黄金三角(INT X,INT Y,INT基地,INT高度)...所以....我很困惑,因为如果我没有弄错(哪些java相信我是...)我用所有适当的值做了一个超级调用,并且是一个构造函数“ClosedShape(boolean,int ,int [],int [])“...继承ClosedShape类:
import java.awt.Graphics;
public abstract class ClosedShape extends Shape {
boolean polygon;
int numPoints;
int[] xVertices;
int[] yVertices;
int x,y,width, height;
public ClosedShape(boolean isPolygon, int numPoints) {
super(0,0);
this.polygon = isPolygon;
this.numPoints = numPoints;
}
public ClosedShape(boolean isPolygon, int numPoints, int[] x, int[] y) {
super(x[0],y[0]);
this.polygon = isPolygon;
if (isPolygon) {
this.numPoints = numPoints;
xVertices = new int[numPoints]; // error check? if x.length == numPoints
//for (int i = 0; i < x.length; i++) { // make copy of array: why?
// xVertices[i] = x[i];
//}
yVertices = new int[numPoints]; // error check? if y.length == numPoints
for (int i = 0; i < y.length; i++) { // make copy of array
yVertices[i] = y[i];
}
}
else { // its an oval - define bounding box
this.numPoints = 4;
this.x = x[0];
this.y = y[0];
width = x[1];
height = y[1];
}
}
public void setXYCoords(int[] x, int[] y){
this.xVertices = x;
this.yVertices = y;
}
// Gives access to the width attribute
public void setWidth(int width){
this.width = width;
}
// Gives access to the height attribute
public void setHeight(int height) {
this.height = height;
}
public void draw(Graphics g) {
if (polygon) {
g.drawPolygon(xVertices, yVertices, numPoints);
}
else {
g.drawOval(x, y, width, height);
}
}
public abstract double Area();
public abstract double Perimeter();
}
我的建议是?相信编译器。你的代码是错误的。 – duffymo 2013-03-01 03:10:30
将第二个构造函数更改为public void Triangle(...)使其不再是构造函数。它现在成为三角形上的一种普通方法。因此,它现在编译是因为你改变了你有问题的构造函数而不是一个构造函数。 – Marshmellow1328 2013-03-01 03:18:52
这是疯狂的建议。如果您需要构造函数,请正确编写它。 – duffymo 2013-03-01 03:23:53