-4

我正在尝试编译此定义,但我不断收到错误消息。错误提示:发现 1 个错误:File: C:\Users\GreatOne\Desktop\Master Folder\04 (j)\04 - Copy\ObjectDemo2\CreateObjectDemo.java [line: 32] Error: origin cannot be resolve to a variable

/**
 * This class offers a main method to create and use sample Point and Rectangle objects.
 */
public class CreateObjectDemo {
    public static void main(String[] args) {

        // Create a point object and two rectangle objects:
        Point origin_one = new Point(23, 94);
        Rectangle rect_one = new Rectangle(origin_one, 100, 200);
        Rectangle rect_two = new Rectangle(50, 100);

        // Display rect_one's width, height, and area:
        System.out.printf("Width of rect_one: " , rect_one.width);
        System.out.printf("Height of rect_one: " , rect_one.height);
        System.out.printf("Area of rect_one: " , rect_one.area());

        // Set rect_two's position:
        rect_two.origin = origin_one;

        // Display rect_two's position:
        System.out.printf("X Position of rect_two: " , rect_two.origin.x);
        System.out.printf("Y Position of rect_two: " , rect_two.origin.y);

        // Move rect_two and display its new position:
        rect_two.move(40, 72);
        System.out.printf("X Position of rect_two: " , rect_two.origin.x);
        System.out.printf("Y Position of rect_two: " , rect_two.origin.y);
        }

        // A method for moving the rectangle:
        public void move(Point newOrigin) {
           origin = newOrigin;
        }
}

/* 
4

5 回答 5

3

编译器发出这些称为错误消息的重要信息。是的,有时它们可​​能很难阅读,但你应该培养阅读它们的技能。

[edwbuck@localhost ~]$ javac CreateObjectDemo.java 
CreateObjectDemo.java:28: error: cannot find symbol
        public void move(Point newOrigin) {
                         ^
  symbol:   class Point
  location: class CreateObjectDemo
CreateObjectDemo.java:5: error: cannot find symbol
        Point origin_one = new Point(23, 94);
        ^
  symbol:   class Point
  location: class CreateObjectDemo
CreateObjectDemo.java:5: error: cannot find symbol
        Point origin_one = new Point(23, 94);
                               ^
  symbol:   class Point
  location: class CreateObjectDemo
CreateObjectDemo.java:6: error: cannot find symbol
        Rectangle rect_one = new Rectangle(origin_one, 100, 200);
        ^
  symbol:   class Rectangle
  location: class CreateObjectDemo
CreateObjectDemo.java:6: error: cannot find symbol
        Rectangle rect_one = new Rectangle(origin_one, 100, 200);
                                 ^
  symbol:   class Rectangle
  location: class CreateObjectDemo
CreateObjectDemo.java:7: error: cannot find symbol
        Rectangle rect_two = new Rectangle(50, 100);
        ^
  symbol:   class Rectangle
  location: class CreateObjectDemo
CreateObjectDemo.java:7: error: cannot find symbol
        Rectangle rect_two = new Rectangle(50, 100);
                                 ^
  symbol:   class Rectangle
  location: class CreateObjectDemo
CreateObjectDemo.java:29: error: cannot find symbol
           origin = newOrigin;
           ^
  symbol:   variable origin
  location: class CreateObjectDemo
8 errors

您在上述课程中有 8 个错误。这就是为什么它不会编译。

错误号 1:

CreateObjectDemo.java:28: error: cannot find symbol
        public void move(Point newOrigin) {
                         ^
  symbol:   class Point
  location: class CreateObjectDemo

在第 28 行,您使用了一个类、接口或枚举,Point而没有告诉编译器Point您正在使用什么。换句话说,您缺少特定Point问题的导入语句,或者没有在CreateObjectDemo.java文件中定义它。

错误 2 和 3:

  symbol:   class Point
  location: class CreateObjectDemo
CreateObjectDemo.java:5: error: cannot find symbol
        Point origin_one = new Point(23, 94);
        ^
  symbol:   class Point
  location: class CreateObjectDemo
CreateObjectDemo.java:5: error: cannot find symbol
        Point origin_one = new Point(23, 94);
                               ^
  symbol:   class Point
  location: class CreateObjectDemo

同样,你正在使用这个Point东西。使用导入让编译器知道在哪里可以找到它。

错误 4、5、6 和 7

  symbol:   class Rectangle
  location: class CreateObjectDemo
CreateObjectDemo.java:6: error: cannot find symbol
        Rectangle rect_one = new Rectangle(origin_one, 100, 200);
                                 ^
  symbol:   class Rectangle
  location: class CreateObjectDemo
CreateObjectDemo.java:7: error: cannot find symbol
        Rectangle rect_two = new Rectangle(50, 100);
        ^
  symbol:   class Rectangle
  location: class CreateObjectDemo
CreateObjectDemo.java:7: error: cannot find symbol
        Rectangle rect_two = new Rectangle(50, 100);
                                 ^
  symbol:   class Rectangle
  location: class CreateObjectDemo
CreateObjectDemo.java:29: error: cannot find symbol

与相同的问题,Point但现在您使用的是未定义的类或接口,称为Rectangle. 使用 import 语句来明确编译器应该在哪里查找 this Rectangle

错误 8:

CreateObjectDemo.java:29: error: cannot find symbol
           origin = newOrigin;
           ^
  symbol:   variable origin
  location: class CreateObjectDemo

现在您正在使用一个名为 an 的变量,origin但您从未告诉编译器您将使用origin(或它的类型是什么)。Point origin;在使用变量之前在某处添加声明origin

于 2013-07-03T03:35:23.380 回答
1

您没有origin在(分别)move方法或CreateObjectDemo类中调用任何局部变量或属性。

如果这就是你需要在你的类中声明一个origin在开头调用的属性:

private Point origin;
于 2013-07-03T03:13:58.470 回答
1

在您的班级中,您没有任何名为 的变量或参数origin

于 2013-07-03T03:24:05.680 回答
0

标准 JavaRectangle类没有办法通过简单的赋值来设置原点,例如:

origin = newOrigin;

而且,最重要的是,该move方法自 Java 1.1 以来已被弃用。

相反,您将需要以下内容:

rect_two.setLocation (origin_one);

有关更多详细信息,请参阅Rectangle 在线 Java 文档

于 2013-07-03T03:21:00.867 回答
0

好的,所以这里的问题是当它需要是类中的一个字段时,它origin被声明为一个局部变量。mainCreateObjectDemo

但真正的问题是你弄乱了代码的缩进。这使得发现origin在错误范围内声明的内容变得更加困难。

第 1 课:花时间正确缩进代码是值得的。从长远来看,您可以节省时间,并且您在代码审查中遇到的挫折也更少。

第 2 课:阅读编译器消息的实际内容是值得的……并尝试理解它在说什么。在这种情况下,消息非常清楚地告诉您问题所在。不幸的是,它不能准确地告诉你你做错了什么。(编译器不擅长弄清楚你要写什么。他们只是遵循说明什么是有效的规则......并告诉你程序是否不是。

于 2013-07-03T03:37:52.097 回答