-1

我正在尝试执行以下程序。我删除了最后一部分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 {

}

4

3 回答 3

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类的完整代码,所以我真的不知道你打算如何实现它......

于 2014-12-07T07:32:10.373 回答
1

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

您需要将 GeometricObject2 的定义移动到它自己的文件中,该文件将被称为 GeometricObject2.java。这应该可以帮助您解决下一个错误。:)

编辑:正如其他海报所指出的,您也可以将抽象类设为非公开,但这对我来说似乎没多大用处。

于 2014-12-07T07:20:04.790 回答
1

如果要在一个文件中保留多个类,则需要删除 public 关键字。所以应该abstract class GeometricObject2没有公众面前。

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

更新

所以现在,对于注释中的类,您的构造函数问题是由于缺少仅接受四个双精度的构造函数。什么会根据您添加的代码修复您的编译将更改为

GeometricObject2 rectangle1 = new MyRectangle2D(1.0, 2.0, 3.0, 4.0, "Red", true);

于 2014-12-07T07:22:56.630 回答