0

Rectangle クラスは、ClosedShape クラスを拡張します。Rectangle のインスタンスを作成しようとすると、次のコンパイラ エラーが発生します。

クラス Rectangle のコンストラクタ Rectangle は、特定の型に適用できません。
      mine = new 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();
}

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

1 に答える 1

4

java.awtパッケージにはRectangle( Rectangle API ) とも呼ばれるクラスがあります。java.awt.Rectangleそのため、Java は、カスタム Rectangle クラスではなく、引数を受け入れないコンストラクターを持つを作成したいと考えています。

これを解決するには、次の 3 つの方法があります。

  1. インポートしないでくださいjava.awt(とにかく使用していないようです)
  2. クラスの名前を Rectangle 以外の名前に変更します
  3. クラスをパッケージに配置Retangleし、 を作成するときに完全なパッケージ名を使用しますRectangle。たとえば、Rectangleクラスをパッケージに入れる場合、新しいを使用してmyshapes作成できますRectanglenew myshapes.Rectangle(10, 50, 90, 50);
于 2013-11-09T02:36:52.253 に答える