为什么这个定义不会编译?

时间:2013-07-03 03:12:34

标签: java

我正在尝试编译此定义,但我一直收到错误。错误说: 找到1个错误: 文件:C:\ Users \ GreatOne \ Desktop \ Master Folder \ 04(j)\ 04 - Copy \ ObjectDemo2 \ CreateObjectDemo.java [line:32] 错误:原点无法解析为变量

/**
 * 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;
        }
}

/* 

5 个答案:

答案 0 :(得分: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语句清楚地说明编译器应该查找此Rectangle

的位置

错误8:

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

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

答案 1 :(得分:1)

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

如果那就是你需要的在你的课程开头声明一个名为origin的属性:

private Point origin;

答案 2 :(得分:1)

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

答案 3 :(得分:0)

标准Java Rectangle类无法通过简单的赋值设置原点,如:

origin = newOrigin;

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

相反,你需要的是:

rect_two.setLocation (origin_one);

有关详细信息,请参阅Rectangle online Java documentation

答案 4 :(得分:0)

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

但真正的问题是你搞砸了代码的缩进。这使得发现origin被宣布在错误的范围内变得更加困难。

第1课:花时间正确缩进代码是值得的。从长远来看,您可以节省时间,并且您的代码审查中的瘀伤也会减少。

第2课:读取编译器消息实际所说的内容是值得的...并尝试理解它的含义。在这种情况下,该消息很清楚地告诉您问题是什么。不幸的是,它无法准确地告诉你你做错了什么。 (编译器不擅长计算你意味着写的东西。他们只是遵循说明有效的规则......并告诉你程序是否没有。)