【问题标题】:Java: constructor Rectangle in class Rectangle cannot be applied to given types, even though the constructor method exists?Java:类 Rectangle 中的构造函数 Rectangle 不能应用于给定类型,即使构造函数方法存在?
【发布时间】:2013-11-09 02:25:31
【问题描述】:

Rectangle 类扩展了 ClosedShape 类。当我尝试创建 Rectangle 的实例时,出现编译器错误:

类 Rectangle 中的构造函数 Rectangle 不能应用于给定 类型;
我的 = 新矩形 (10, 50, 90, 50);
必需:无参数
找到:int、int、int、int
原因:实际和形式参数列表 长度不同

但是我试图调用的构造函数确实存在!!为什么会这样?见以下代码:

ClosedShape.java:

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 () {}

    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();
}

矩形.java:

public class Rectangle extends ClosedShape {

    public void Rectangle(int x, int y, int width, int height) {
        super(true, 4);
    }

    public double Area() {
        return 0.0;
    }

    public double Perimeter() {
        return 0.0;
    }
}

DrawingFrame.java(测试器类):

import java.awt.*;
import javax.swing.Timer;

public class DrawingFrame extends javax.swing.JPanel {
    public DrawingFrame (int w, int h) {
        setFocusable(true);
        setBackground(Color.black);
        setSize(w,h);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        //tester  : Remove comments to test classes
        Shape mine;
        g.setColor(Color.red);
        mine.draw(g);

        mine = new Rectangle(10, 50, 90, 50);
        g.setColor(Color.yellow);
        mine.draw(g);
    }
}

这里是 Shape.java,以备不时之需:

import java.awt.*;

public abstract class Shape {

    int initX, initY;
    Color fillColour;

    public Shape() {
        initX = 0;
        initY = 0;
        fillColour = null;
    }

    public Shape(int x, int y) {
        initX = x;
        initY = y;
    }

    public void setInitX (int x) {
        initX = x;
    }

    public void setInitY (int y) {
        initY = y;
    }

    public abstract void draw(Graphics g);
    public abstract double Area();
    public abstract double Perimeter();
}

【问题讨论】:

    标签: java


    【解决方案1】:

    java.awt 包中有一个类,也称为Rectangle (Rectangle API)。所以Java 认为你想创建一个java.awt.Rectangle,它有一个不接受参数的构造函数,而不是你自定义的Rectangle 类。

    有三种方法可以解决这个问题:

    1. 不要导入java.awt(反正你好像没用过)
    2. 将您的类重命名为 Rectangle 以外的其他名称
    3. Retangle 类放入包中,并在创建Rectangles 时使用完整的包名。例如,如果您将 Rectangle 类放在包 myshapes 中,您可以使用 new myshapes.Rectangle(10, 50, 90, 50); 创建一个新的 Rectangle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多