英文:
Java - Cannot resolve constructor
问题
我是Java编程新手。我有三个类,分别是 Main、Point 和 Rectangle。我可以使用 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;
    //构造函数
    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;
    //四个构造函数
    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;
    }
    // 移动矩形的方法
    public void move(int x, int y) {
        origin.x = x;
        origin.y = y;
    }
    // 计算矩形面积的方法
    public int getArea() {
        return width * height;
    }
}
英文:
I am new to Java programming. I have three classes which are Main, Point and Rectangle. I can use all constructors in Rectangle class except this one: Rectangle(Point p, int w, int h). Java compiler gives: "Cannot resolve constructor 'Rectangle(java.awt.Point, int, int)'" error. Thanks.
Here my classes:
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;
    }
}
答案1
得分: 4
你的答案相对简单。你导入了错误的包,而不是导入java.awt.Rectangle,应该导入你自己的类。
通过这样做:
import <你的项目名称>.Rectangle;
如果我在Eclipse中有一个名为MyShapes的项目,其中包含相同的类,我会这样导入:
import MyShapes.Rectangle;
当然,这取决于你的文件结构是如何的,如果它在一个子文件夹(子包)中,我会这样做:
import MyShapes.ShapesClasses.Rectangle;
如果你计划使用自己的Point类而不是Java.awt的Point类,同样的规则也适用!
英文:
Your answer is fairly simple. You've imported the wrong import, rather than import java.awt.Rectangle, import your own class.
By doing:
import <nameOfTheProject>.Rectangle;
If I had a project in Eclipse called MyShapes and had the same classes. I would import it like:
import MyShapes.Rectangle;
Now this obviously depends on how your file structure is, but if it's inside a sub-folder (sub-package), I would do like:
import MyShapes.ShapesClasses.Rectangle;
This also applies to the Point class if you're planning on using your own Point class and not Java.awt's one!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论