2013-03-01 104 views
1

所以我在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(); 








} 
+4

我的建议是?相信编译器。你的代码是错误的。 – duffymo 2013-03-01 03:10:30

+0

将第二个构造函数更改为public void Triangle(...)使其不再是构造函数。它现在成为三角形上的一种普通方法。因此,它现在编译是因为你改变了你有问题的构造函数而不是一个构造函数。 – Marshmellow1328 2013-03-01 03:18:52

+0

这是疯狂的建议。如果您需要构造函数,请正确编写它。 – duffymo 2013-03-01 03:23:53

回答

1

我终于明白了。我正在调用一个我甚至不需要使用的构造函数!我只需要对第一个构造函数的调用,然后使用setXYCoords()方法做什么,我需要用数组做....继承人我的最终代码:

(ClosedShape.java保持不变)

import java.awt.Graphics; 

public class Triangle extends ClosedShape{ 

    public Triangle(int x, int y, int base, int height){ 
     super(true, 3); 

     setWidth(base); 
     setHeight(height); 

     int [] arrayX = new int[3]; 
     arrayX[0] = x; 
     arrayX[1] = (x+(width/2)); 
     arrayX[2] = (x+width); 

     int [] arrayY = new int[3]; 
     arrayY[0] = y; 
     arrayY[1] = (y-height); 
     arrayY[2] = y; 

     setXYCoords(arrayX, arrayY); 

    } 

    public double Area(){ 
       return 0; 
    } 

    public double Perimeter(){ 
     return 0; 
    } 


} 
4

问题是ClosedShape没有默认的无参数构造函数。

看这个构造函数:

public Triangle(int x, int y, int base, int height){ 

有一个super()构造函数没有显式调用,因此编译器假定它需要调用无参数的构造函数。但没有一个...

+0

更好地检查自己,user93353。这是另一个问题,而不是你正在看的问题。 – duffymo 2013-03-01 03:15:05

+0

我明白我在那里做错了什么...我编辑它,并把我的单一改变哈哈,如果我没有弄错现在应该罚款...我希望哈哈哈它编译:)谢谢! – MicroMumbler 2013-03-01 03:21:34

+0

那么有什么变化?为ClosedShape写一个没有参数的构造函数,或者更好的是,向Triangle添加正确的超级调用? – duffymo 2013-03-01 03:24:54

-1

正如@duffymo所述,如果您没有明确地调用super(),编译器会将调用插入no-arg构造函数。

我认为你要找的解决方案是工厂方法。例如,您可以创建一个静态方法,即createTriangle(int x, int y, int base, int height)。在该方法中构建数组,调用相应的构造函数,然后返回构造的对象。

+0

他不需要使用静态方法或工厂。他需要仔细观察ClosedShape类。 – Marshmellow1328 2013-03-01 03:19:28

+0

我不认为从Marshmellow1328的建议是最好的根据上面的评论。 – duffymo 2013-03-01 03:25:36

相关问题