我无法弄清楚什么是错的,得到'类型已经定义的错误'

时间:2014-04-01 19:20:13

标签: java types

好的,我正在使用我在网上购买的一本书学习java,由于某种原因,Java不会允许这个程序工作,即使它是书中的确切文本。有人可以解释为什么我一直被告知'SimpleCircle类型已被定义'?它将此显示为“SimpleCircle(){radius = 1;}”

行旁边的错误
public class SimpleCircle {
    /** Main method */
    public static void main(String[] args) {
        //create a circle with radius 1
        SimpleCircle circle1 = new SimpleCircle();
        System.out.println("The area of the circle of radius " 
                + circle1.radius + " is " + circle1.getArea());

        //create a a circle with radius 25
        SimpleCircle circle2 = new SimpleCircle(25);
        System.out.println("The area of the circle of radius " 
                + circle2.radius + " is " + circle2.getArea());

        //create a circle with radius 125
        SimpleCircle circle3 = new SimpleCircle(125);
        System.out.println("the area of the circle of radius " 
                + circle3.radius + " is " + circle3.getArea());

        //modify circle radius
        circle2.radius = 100;
        System.out.println("The area of the circle of radius " 
                + circle2.radius + " is " + circle2.getArea());
    }

    double radius;

    /** construct a circle with radius 1 */
    SimpleCircle() {
        radius = 1;
    }

    /** construct a circle with a specified radius */
    SimpleCircle(double newRadius) {
        radius = newRadius;
    }

    // return the area of this circle
    double getArea() {
        return radius * radius * Math.PI;
    }

    // return the perimeter of the circle
    double getPerimeter() {
        return 2 * radius * Math.PI;
    }

    // set a new radius for this circle
    void setRadius(double newRadius) {
        radius = newRadius;
    }
}

1 个答案:

答案 0 :(得分:-1)

我也遇到了同样的错误。原因是你工作的文件夹包含另一个文件(当前工作文件除外),其中使用了同一个类(这里是SimpleCircle)。简单地修改了在当前文件中的类名(例如:SimpleCircle到SimpleCircle2),然后错误就会消失。