2014-12-07 165 views
-1

我正试图做下面的程序。为了将其缩短一点,我删除了MyRectangle2D的最后一部分。 当我尝试编译时,我得到2个错误,我只是无法通过我的方式!无法编译,类未声明Java

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 { 

}

+0

你甚至没有读错误信息... – 2014-12-07 07:24:56

回答

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

+0

非常感谢您的回复!但编译器仍然给我错误,即使我改变了变量。 :(任何想法为什么? – Avacay 2014-12-07 07:38:44

+0

上面的代码为我编译。什么是错误信息和你改变了什么? – bmk 2014-12-07 07:42:32

+0

我刚刚改变了变量!与你完全一样。 TestExample.java:11:找不到符号 符号:构造MyRectangle2D(java.lang.String中,布尔) 位置:类MyRectangle2D GeometricObject2 rectangle1 =新MyRectangle2D( “红”,真正的); 1错误 – Avacay 2014-12-07 07:45:44

1

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

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

编辑:正如其他海报已经指出,你也可以使抽象类非公开,但这似乎不是对我有用。

+0

非常感谢你!这使差异..我仍然看到它!为什么它更有意义?我认为编译器默认情况下做了类? – Avacay 2014-12-07 07:36:55

+0

简而言之,它与类加载器找到类的方式有关。如果你引用一个类,它将检查你的类路径,看它是否已经存在。如果没有,它将查找名称正确的文件,并尝试编译该文件。 – 2014-12-07 07:39:38

+0

“我认为默认情况下编译器创建了类?”编译器只编译它需要的东西 - 如果你制作公共类Foo,Bar和Baz,每个都在他们自己的文件中,而Foo指向Bar但是既不指向Baz,那么当你编译Foo时,你会发现Bar是编译成Bar.class,但Baz不是。 – 2014-12-07 07:41:28

1

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

你的第二个错误中提到的失踪构造,但对于这一点,你就需要为MyRectangle2D类提供代码

UPDATE

所以现在,在注释类,你的构造问题是由于缺少的构造函数只接受四个双打。有什么根据您添加的代码将解决你的编译woulb被更改为

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

+0

这是MyRectangle2D的代码条:) class MyRectangle2D扩展GeometricObject2 private double x = 0; private double y = 0; private double width = 0; 私人双倍高度= 0; MyRectangle2D(){} MyRectangle2D(双X,双Y,双宽度,双高度,串色,布尔填充){ 超级(颜色,填充的); this.x = x; this.y = y; this.width = width; this.height = height; – Avacay 2014-12-07 07:50:25