构造函数...不适用(实际和形式参数列表的长度不同) - 但它确实匹配

时间:2013-03-01 03:08:28

标签: java constructor arguments argument-passing

所以我在java中为学校做作业......这是一个类层次结构的赋值,我们应该创建一个“Triangle.java”类,它扩展了一个“ClosedShape.java”类,它扩展了一个“ Shape.java“.... ClosedShape和Shape都给了我们,所以很可能它们没什么问题(我会发布它们)但是我的Triangle类如下:

    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

它还在赋值中指定三角形的签名必须为Traingle(int x,int y,int base,int height)...所以....我很困惑因为如果我没有弄错(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();








}

3 个答案:

答案 0 :(得分:4)

问题是ClosedShape没有默认的no-arg构造函数。

看看这个ctor:

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

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

答案 1 :(得分: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;
    }


}

答案 2 :(得分:-1)

正如@duffymo所说,如果你没有显式调用super(),编译器将插入对no-arg构造函数的调用。

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