Java-无法解析构造函数

Shmn:

我是Java编程的新手。我有三类它们是MainPointRectangle我可以在Rectangle类中使用除以下一个以外的所有构造函数Rectangle(Point p, int w, int h)Java编译器给出:“无法解析构造函数'Rectangle(java.awt.Point,int,int)'”错误。谢谢。

这是我的课程:

Main.java

import java.awt.*;
import java.awt.Point;
import java.awt.Rectangle;

public class Main {

    public static void main(String[] args) {
        Point originOne = new Point(5,10);   
        Rectangle rectOne = new Rectangle(originOne, 100, 200);
    }

}

Point.java

public class Point {
    public int x = 0;
    public int y = 0;
    //contructor
    public Point(int a, int b){
        x = a;
        y = b;
    }
}

Rectangle.java

public class Rectangle {
    public Point origin;
    public int width = 0;
    public int height = 0;

    //four contructors
    public Rectangle() {
        origin = new Point(0, 0);
    }
    public Rectangle(Point p){
        origin = p;
    }
    public Rectangle(int w, int h){
        origin = new Point(0,0);
        width = w;
        height = h;
    }
    public Rectangle(Point p, int w, int h) {
        origin = p;
        width = w;
        height = h;
    }

    // a method for moving the rectangle
    public void move(int x, int y) {
        origin.x = x;
        origin.y = y;
    }

    //a method for computing the area of rectangle
    public int getArea() {
        return width * height;
    }
}
杰西·吉拉多(Jessy Guirado):

您的答案很简单。您导入了错误的导入,而不是import java.awt.Rectangle而是导入了自己的类。

通过做:

import <nameOfTheProject>.Rectangle;

如果我在Eclipse中有一个名为MyShapes的项目,并且具有相同的类。我将其导入为:

import MyShapes.Rectangle;

现在,这显然取决于您的文件结构,但是如果它在子文件夹(子包)中,则需要:

import MyShapes.ShapesClasses.Rectangle;

如果您打算使用自己的Point类而不是Java.awt的Point类,这也适用于Point类。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章