无法编译,类没有声明Java

时间:2014-12-07 07:14:59

标签: java jgrasp

我正在尝试按照程序执行此操作。我删除了MyRectangle2D的最后一部分,以便将其缩短一点。 当我尝试编译时,我得到2个错误,我无法完成工作!

TestExample.java:17: class GeometricObject2 is public, should be declared in a file named GeometricObject2.java
public abstract class GeometricObject2 {

TestExample.java:11: cannot find symbol
symbol  : constructor MyRectangle2D(double,double,double,double)
location: class MyRectangle2D
      GeometricObject2 rectangle1 = new MyRectangle2D(1.0, 2.0, 3.0, 4.0);

2 errors

非常感谢帮助!

import java.util.*;

public class TestExample
{
   public static void main(String[] args)
   {
      GeometricObject2 rectangle1 = new MyRectangle2D(1.0, 2.0, 3.0, 4.0);
      System.out.println(rectangle1.getArea());
   }
}


public abstract class GeometricObject2 {
  private String color = "white";
  private boolean filled;


  protected GeometricObject2() {
  }


  protected GeometricObject2(String color, boolean filled) {
    this.color = color;
    this.filled = filled;
  }

  public String getColor() {
    return color;
  }

  public void setColor(String color) {
    this.color = color;
  }

  public boolean isFilled() {
    return filled;
  }

  public void setFilled(boolean filled) {
    this.filled = filled;
  }

  public abstract double getArea();

  public abstract double getPerimeter();
}



class MyRectangle2D extends GeometricObject2 {

}

3 个答案:

答案 0 :(得分:2)

如果你想在TestExample.java文件中实现所有内容,那么我会尝试这样的事情:

import java.util.*;

public class TestExample
{
    public static void main(String[] args)
    {
        GeometricObject2 rectangle1 = new MyRectangle2D(2, 2, 3, 4, "Red", true);
        System.out.println(rectangle1.getArea());
    }
}


abstract class GeometricObject2 {
    private String color = "white";
    private boolean filled;


    protected GeometricObject2() {
    }


    protected GeometricObject2(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public abstract double getArea();

    public abstract double getPerimeter();
}



class MyRectangle2D extends GeometricObject2
{
    private double x;
    private double y;
    private double width;
    private double height;

    public MyRectangle2D(double x, double y, double width, double height,
                         String color, boolean filled) {
        super(color, filled);
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public double getArea() {
        return width * height;
    }

    public double getPerimeter() {
        return 2 * (width + height);
    }
}

你没有发布MyRectangle2D类的完整代码,所以我真的不知道你打算如何实现它......

答案 1 :(得分:1)

Java要求在名为Foo.java的源文件中定义一个名为Foo的公共类 - 这意味着您不能在同一.java文件中声明两个公共类(因为该文件只有一个名称)

您需要将GeometricObject2的定义移动到其自己的文件中,该文件将被称为GeometricObject2.java。这应该会让你完成下一个错误。 :)

编辑:正如其他海报所说,你也可以使抽象类非公开,但这对我来说似乎不太有用。

答案 2 :(得分:1)

如果要在一个文件中保留多个类,则需要删除public关键字。因此,如果没有公众,abstract class GeometricObject2应该是GeometricObject2 rectangle1 = new MyRectangle2D(1.0, 2.0, 3.0, 4.0, "Red", true);

你的第二个错误提到了一个缺少的构造函数,但为此你需要提供MyRectangle2D类的代码

更新

现在,使用注释中的类,构造函数问题是由于缺少只接受四个双精度的构造函数。根据你添加的代码修改你的编译将会改变为

{{1}}